0

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?

Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
  • What are you returning from the django side? – Burhan Khalid Feb 17 '13 at 07:59
  • Burhan, how can I check what is returning from the Django side? The function in Django that is supposed to be called looks like this: @dajaxice_register def sayhello(request): return simplejson.dumps({'message':'Hello World'}) – Saqib Ali Feb 17 '13 at 09:07
  • how are you running the code, using runserver? do you have debug=True? – Burhan Khalid Feb 17 '13 at 09:13
  • In my settings.py, DEBUG = True. This is how I run the Django server: python ~myUser/project_base/web_container/manage.py runserver 0.0.0.0:80 FYI, when the onclick method above fires, I see the following line appear from the Django webserver: "POST /dajaxice/myApp.sayhello/ HTTP/1.1" – Saqib Ali Feb 17 '13 at 09:28

3 Answers3

4

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" %}
ChrisFreeman
  • 5,831
  • 4
  • 20
  • 32
1

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'})
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • When I changed it to a GET request as you suggested, it still didn't work. Same symptom. However, this time when the onclick event fires I see this coming from the server window: [17/Feb/2013 13:12:18] "GET /dajaxice/myApp.sayhello/?argv=undefined HTTP/1.1" 200 10567 Furthermore, I put a print statement inside sayhello(). If it reached there, it should print that line onto the server's console. But it didn't. So It's not getting to the sayhello() function. – Saqib Ali Feb 17 '13 at 18:15
0

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