Sourabh Dev
Sourabh Dev

Reputation: 743

Dajax not working

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')),

)

Upvotes: 0

Views: 718

Answers (3)

Trix
Trix

Reputation: 597

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

Upvotes: 1

0x3bfc
0x3bfc

Reputation: 3151

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

Upvotes: 0

Sourabh Dev
Sourabh Dev

Reputation: 743

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.

Upvotes: 0

Related Questions