Dajax not working

若如初见. 提交于 2020-01-30 10:36:14

问题


Dajax is not working, I am not able to understand why. I am using Django 1.7

My ajax.py file looks this:

from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def jmc_foundation_tower_number(request, option):

    print("It works!")

My template call is as follows:

<div class='col-lg-3'>
  <select id='id_tower_number' name='tower_number' onchange="Dajaxice.core.views.jmc_foundation_tower_number(Dajax.process, {'option':$this.value})" onclick="Dajaxice.core.views.jmc_foundation_tower_number(Dajax.process, {'option':$this.value})" class='form-control'>
       {% for tower in towers %}
          <option value='{{ tower }}'>{{ tower }}</option>
       {% endfor %}
   </select>
</div>

My urls.py is as follows:

from django.conf.urls import patterns, include, url
from django.contrib import admin

from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()


urlpatterns = patterns('',
    url(r'^index$', 'core.views.index', name='index'),

    url(r'^admin/', include(admin.site.urls)),
    url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),

)

回答1:


django-dajax and django-dajaxice

Should I use django-dajax or django-dajaxice?

In a word, No. I created these projects 4 years ago as a cool tool in order to solve one specific problems I had at that time.

These days using these projects is a bad idea. (...) If you want to use this project, you are probably wrong. You should stop couplig your interface with your backend or... in the long term it will explode in your face.

jorgebastida/django-dajax




回答2:


Apparently javascript function names with underscores('_') don't work when using functions like onclick.It's better to use functions like somefunction() instead of some_function() to make Dajax work.




回答3:


The following is an example to make Helloworld using dajax

models.py:

 from django.db import models
 from dajax.core import Dajax
 from dajaxice.decorators import dajaxice_register
 @dajaxice_register
 def say_hello(request,value):
    dajax = Dajax()
    dajax.alert(value)
    return dajax.json()

urls.py:

urlpatterns = patterns('',
    # Examples:
    url(r'^$', 'server.views.index', name='index'),

where "server" in server.views.index is the application name inside your project

in index.html file you have to use a jquery function to call this dajax request as following index.html:

 <html>
    <script>
      function print_helloworld(){
                var value = Dajaxice.server.models.say_hello(Dajax.process,{'value':'Hello World!'});
            }
    </script>
    <body>
       <button id='mybtn' class='btn btn-primary' onclick='print_helloworld()'>Hello World</button>
    </body>
 </html>

if you need to access the index file from public ip you have to add the {% csrf_token %} token before button



来源:https://stackoverflow.com/questions/26219391/dajax-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!