【问题标题】:Django: STATIC_URL adds appname to the urlDjango:STATIC_URL 将 appname 添加到 url
【发布时间】:2011-11-10 13:48:28
【问题描述】:

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

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,但是当我到达由我的 profiles 应用程序处理的 localhost:8000/profile/edit 站点时,{{ STATIC_URL }} 将所有路径从 /static/... 更改为 @987654330 @,所以我的 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>

【问题讨论】:

  • staticfiles_urlpatterns()?这是什么?
  • @Thibault J:根据this,在开发环境中提供静态文件是必需的。
  • 如您提供的链接中所述,这仅在您不使用 django 内置服务器时有效。否则,将自动提供静态文件。您使用的是哪个服务器?
  • 你能举个例子说明你在个人资料模板上有 {{ STATIC_URL }} 的地方吗?
  • @Thibault J:是的,我只是在开发过程中使用manage.py runserver。 @Jordan:已编辑问题。

标签: python django django-staticfiles


【解决方案1】:

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

urlpatterns += staticfiles_urlpatterns()

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

编辑

如果settings.py,试试这样的:

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

【讨论】:

  • 这仍然将/profile 添加到静态路径中,因此无法解决我的问题。
  • SITE_ROOT的值是多少?
  • ...那个不在我的settings.py 中,我应该设置它吗?
  • STATIC_ROOT 应该是服务器上静态目录的绝对路径。尝试这样的事情:(编辑我的答案)
猜你喜欢
  • 1970-01-01
  • 2014-07-16
  • 2011-12-15
  • 2020-08-02
  • 2015-01-25
  • 2012-03-31
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多