Django:STATIC_URL将应用名称添加到网址 [英] Django: STATIC_URL adds appname to the url

查看:179
本文介绍了Django:STATIC_URL将应用名称添加到网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have configured my static settings like so:

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 网站(由我的个人资料应用程序处理), {{STATIC_URL}} 更改了 / static /... / profile / static /...,因此找不到我的JavaScript和样式表

It works very well for the url localhost:8000/login, but when I get to the localhost:8000/profile/edit site, which is handled by my profiles App, the {{ STATIC_URL }} changes all the paths from /static/... to /profile/static/..., so my javascripts and stylesheets are not found any more.

我要怎么做?

编辑:在这里是我的 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 中删除​​以下行:

Since you're using the django built-in developement server, try to remove the following line from your urls.py:

urlpatterns += staticfiles_urlpatterns()

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

In production, you'de better not serve static files with django, so use the collectstatic command.

编辑

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

If settings.py, try something like this:

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

这篇关于Django:STATIC_URL将应用名称添加到网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆