Saqib Ali
Saqib Ali

Reputation: 12585

How to get simple, Hello World Dajaxice interaction to work?

I'm trying to get Django to produce working Ajax interactions on my web page.

I have followed the instructions for django-dajaxice here: http://django-dajaxice.readthedocs.org/en/latest/quickstart.html and here: http://django-dajaxice.readthedocs.org/en/latest/installation.html

However, when I try to run that Ajax code in Chrome, all I get is a dialog box popped up with the word "undefined" in it

BTW, this is how I am invoking Ajax from the HTML page:

<script type="text/javascript">
    function my_js_callback(data){
        alert(data.message);
    }
</script>

AJAX!!<br/>
<br/>
<input id="myID" type="text" name="myID" maxlength="255" onclick="Dajaxice.example.sayhello(my_js_callback);"/>
<br/>

So why am I getting this undefined dialog box? I opened up Chrome's debugger and it doesn't show any errors in this case.

Incidentally, when the installation instructions above tell me to modify my urls.py, that is ambiguous. I have two urls.py; one for my site and one for my application. I put those statements in the site's url.py. But I wasn't sure if that was correct. Can someone please confirm or deny?

Upvotes: 0

Views: 1999

Answers (3)

ChrisFreeman
ChrisFreeman

Reputation: 6329

I was also struggling with the dajaxice example when following the installation and quickstart references you cite. My error was similar, though I was seeing "Dajaxice.example not defined."

Let's call the main django project myproj and the installed app where the ajax.py file is located myproj/myapp. The two parts that seemed to get it working for me were:

  1. Place ajax.py file containing def sayhello(request) in correct directory and use the corresponding correct path for the sayhello function.

    • Place it in a directory for one of the apps listed in the settings.INSTALLED_APPS
    • If your installed app name is listed as "myproj", the ajax file should be myproj/ajax.py, the js reference should be: Dajaxice.myproj.sayhello(my_js_callback)
    • If your installed app name is listed as "myproj.subapp", the ajax file should be myproj/myapp/ajax.py, the js reference should be: Dajaxice.myproj.myapp.sayhello(my_js_callback) [I used this one]
    • Remember to put the {% dajaxice_js_import %} in the template <head> section

    Note: In my example, "myapp" could be replaced with "example" to match the dajaxice example code.

  2. Setting up collectstatic correctly to generate a new /static/dajaxice/dajaxice.core.js file when as I updated by code.

    • I followed the directions exactly as listed in the dajaxice readthedocs installation link you referenced.
    • Added STATIC = '/static/ in the settings.py file
    • Added 'django.contrib.staticfiles' to INSTALLED_APPS in settings.py file
    • ran python manage.py collectstatic to pull all of the files into the /static/ directory. This includes the generated file /static/dajaxice/dajaxice.core.js
    • Inspecting this file, you should see the object hierarchy for the sayhello function as: Dajaxice.myproj.sayhello or Dajaxice.myproj.myapp.sayhello.

Also, if you are trying the multiply example from http://www.dajaxproject.com/multiply/, change function calculate() to match the location of your ajax.py file to either:

<script type="text/javascript" charset="utf-8">
    function calculate(){
        Dajaxice.myproj.myapp.multiply(Dajax.process,{'a':$('#a').val(),'b':$('#b').val()})
    };
</script>

or

<script type="text/javascript" charset="utf-8">
    function calculate(){
  Dajaxice.myproj.multiply(Dajax.process,{'a':$('#a').val(),'b':$('#b').val()})
    };
</script>

and include the appropriate dajax js file in the <head> section:

   <script src="/static/dajax/jquery.dajax.core.js"></script>

or

  {% static "/static/dajax/jquery.dajax.core.js" %}

Upvotes: 4

user2990631
user2990631

Reputation: 1

My error: I use debug in chrome(F12), Dajaxice is available, but Dajaxice.example and Dajaxice.example.sayHello is undifined.

you should install your APP example in your settings.py

http://django-dajaxice.readthedocs.org/en/latest/quickstart.html

INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'dajaxice',
        'example',
        ...
)  

note example/ajax.py, you can not put the code into other file.

cat example/ajax.py

#!/usr/bin/env python
#coding:utf-8
from django.utils import simplejson
from dajaxice.decorators import dajaxice_register


@dajaxice_register()
def sayhello(request):
    return simplejson.dumps({'message':'Hello World'})

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174622

You are sending a POST request, and most likely the CSRF middleware is blocking it. Instead, send a GET request by modifiying the method decorator:

@dajaxice_register(method='GET')
def sayhello(request):
    return simplejson.dumps({'message':'Hello World'})

Upvotes: 1

Related Questions