[英]Django Ajax - Dajaxice.myapp is not defined
我正在嘗試在我的網站中實現Ajax功能。 這似乎是一個中等水平的問題,但是我在網上找到的所有解決方案都很簡單,就像忘記了教程中的某些內容一樣。 錯誤消息出現在JavaScript控制台中。
我正在嘗試遵循本教程: http : //django-dajaxice.readthedocs.org/en/latest/installation.html
我的動作:
我使用pip install django_dajaxice
進行安裝
我將本教程中的settings.py
和urls.py
代碼復制粘貼到自己的代碼中:
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = patterns('',
# AJAX
url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
)
urlpatterns += staticfiles_urlpatterns()
和
STATICFILES_FINDERS = ( ... ) # Exact copy paste from tutorial
TEMPLATE_LOADERS = ( ... ) # Exact copy paste from tutorial
TEMPLATE_CONTEXT_PROCESSORS = ( ... ) # Exact copy paste from tutorial
我在base.html
包括了模板標簽,實際的html文件是從那繼承來的
然后快速入門: http : //django-dajaxice.readthedocs.org/en/latest/quickstart.html
我創建了content / ajax.py(內容是我的應用)。 里面的代碼很簡單:
from dajax.core import Dajax
from content import models
from dajaxice.decorators import dajaxice_register
@dajaxice_register
def fav(request):
dajax = Dajax()
return dajax.json()
最后,實例化AJAX的JS和HTML代碼:
<script type="text/javascript" src="{{ STATIC_URL }}dajaxice/dajaxice.core.js"></script>
function js_callback(data) {
Dajax.process(data);
alert(data.message);
}
<a onClick="Dajaxice.content.fav(js_callback);">Favorite</a>
您需要再次運行collectstatic
來重新生成dajaxice.core.js
。 也許您應該刪除之前已收集的靜態文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.