Django:STATIC_URL为网址添加了appname

时间:2011-09-05 11:41:59

标签: python django django-staticfiles

我已经配置了我的静态设置:

STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    ('js', os.path.join(STATIC_ROOT, 'js')),
    ('css', os.path.join(STATIC_ROOT, 'css')),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#   'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

和我urls.py中的这些:

urlpatterns = patterns('',
    url(r'^login/?$', login, name='login'),
    url(r'^logout/?$', logout_then_login, name='logout'),

    url(r'^profile/(?P<user_id>\d+)$', 'profiles.views.detail'),
    url(r'^profile/edit$', 'profiles.views.edit'),
)

urlpatterns += staticfiles_urlpatterns()

它适用于网址localhost:8000/login,但是当我到达localhost:8000/profile/edit网站时,我profiles应用处理了{{ STATIC_URL }}网站从/static/.../profile/static/...的路径,因此找不到我的javascripts和样式表。

我做错了什么?

编辑:这是我的base.html

<!DOCTYPE html>
<html>
    <head>
        <title>Neighr{% block title %}{% endblock %}</title>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <script type="text/javascript" src="{{ STATIC_URL }}js/jquery.min.js"></script>
        {% block script %}{% endblock %}
    </head>
    <body>
        {% block content %}{% endblock %}
    </body>
</html>

1 个答案:

答案 0 :(得分:2)

由于您使用的是django内置开发服务器,请尝试从urls.py中删除以下行:

urlpatterns += staticfiles_urlpatterns()

在制作中,你最好不要使用django提供静态文件,所以使用collectstatic命令。

修改

如果settings.py,请尝试以下方式:

STATIC_ROOT = os.path.join(os.path.dirname(__file__), '../../static')
STATIC_URL = '/static/'