如何告诉我的django模板静态文件(dajax.core.js)在哪里?

时间:2011-10-08 17:06:16

标签: python django templates

我正在尝试在我的Django应用程序中使用Dajax,但是当我尝试将其包含在我的html模板中时,我收到了404错误。我在Windows 7上安装了Dajax和Dajaxice。这是我的STATIC_URL的样子:

STATIC_URL = '/static/'

我的静态文件夹放在我的app文件夹中,所以从Eclipse项目的src文件夹中,静态文件夹的路径是:

appname/static

我已将以下javascript文件放入静态目录中的“js”目录中:

dajaxice.core.js
jquery.dajax.core.js

在我的模板的head标记内是我的脚本标记,如下所示:

<script src="{{ STATIC_URL }}js/jquery.dajax.core.js" type="text/javascript" charset="UTF-8"></script>

但是,当我打开模板时,出现以下错误:

GET http://localhost:8000/dajax/js/jquery.dajax.core.js 404 (NOT FOUND)

为什么找不到jquery.dajax.core.js?谢谢你的帮助!

编辑此外,这是我的观点:

from django.shortcuts import render_to_response
from django.template import RequestContext

def dajax(request):
    return render_to_response('dajax.html', context_instance=RequestContext(request))

这很简单,因为我只是在测试。

1 个答案:

答案 0 :(得分:0)

我认为这是因为您需要为django添加静态服务器(仅用于本地运行)

将此添加到您的urls.py:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_PATH}),
    )

或者您需要将MEDIA_ROOT指向静态文件夹的绝对路径。

否则,我猜您在设置中声明了STATIC_URL两次,因为您说您将其设置为/static/,但模板似乎正在用dajax替换它

GET http://localhost:8000/dajax/js/jquery.dajax.core.js 404 (NOT FOUND)

应为http://localhost:8000/static/js/jquery.dajax.core.js