小编典典

Django:STATIC_URL将应用名称添加到网址

python

我已经配置了我的静态设置,如下所示:

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()

它对于url效果很好localhost:8000/login,但是当我到达localhost:8000/profile/edit由我的profiles应用程序处理的网站时,将{{ STATIC_URL }}所有路径从更改/static/.../profile/static/...,因此不再找到我的JavaScript和样式表。

我怎么了

编辑 :这将是我的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>

阅读 139

收藏
2021-01-20

共1个答案

小编典典

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

urlpatterns += staticfiles_urlpatterns()

在生产中,最好不要使用django提供静态文件,因此请使用collectstatic命令。

编辑

如果为settings.py,请尝试以下操作:

STATIC_ROOT = os.path.join(os.path.dirname(__file__), '../../static')
STATIC_URL = '/static/'
2021-01-20