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:
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.
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" %}