'Django Ajax - Dajaxice myapp未定义'

2024-06-28 19:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的网站中实现Ajax功能。这似乎是一个中等常见的问题,但我在网上找到的所有解决方法都很简单,比如忘记了教程中的某些内容。错误消息出现在JavaScript控制台中。在

我试着按照这个教程:http://django-dajaxice.readthedocs.org/en/latest/installation.html

我的行动:

我使用pip install django_dajaxice进行安装

我复制粘贴了教程中的settings.pyurls.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()

以及

^{pr2}$

我在我的base.html中包含了模板标记,实际的html文件就是从中继承的

然后快速启动:http://django-dajaxice.readthedocs.org/en/latest/quickstart.html

我创建了内容/阿贾克斯.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>

Tags: django代码frompycoreimport内容html