编程语言
首页 > 编程语言> > python – Django:STATIC_URL将urname添加到url

python – Django:STATIC_URL将urname添加到url

作者:互联网

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

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站点(由我的配置文件App处理)时,{{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>

解决方法:

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

urlpatterns += staticfiles_urlpatterns()

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

编辑

如果settings.py,尝试这样的事情:

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

标签:django-staticfiles,python,django
来源: https://codeday.me/bug/20191008/1872278.html