Django:STATIC_URL 将应用程序名称添加到 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
站点,由我的 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>
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',
)
and these in my urls.py
:
urlpatterns = patterns('',
url(r'^login/?
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.
What'd I do wrong?
EDIT: Here would be my 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>
, login, name='login'),
url(r'^logout/?
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.
What'd I do wrong?
EDIT: Here would be my base.html
, logout_then_login, name='logout'),
url(r'^profile/(?P<user_id>\d+)
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.
What'd I do wrong?
EDIT: Here would be my base.html
, 'profiles.views.detail'),
url(r'^profile/edit
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.
What'd I do wrong?
EDIT: Here would be my base.html
, 'profiles.views.edit'),
)
urlpatterns += staticfiles_urlpatterns()
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.
What'd I do wrong?
EDIT: Here would be my base.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用的是 django 内置开发服务器,请尝试从 urls.py 中删除以下行:
在生产中,您最好不要使用 django 提供静态文件,因此请使用 <代码>collectstatic命令。
编辑
如果
settings.py
,请尝试如下操作:Since you're using the django built-in developement server, try to remove the following line from your
urls.py
:In production, you'de better not serve static files with django, so use the
collectstatic
command.edit
If
settings.py
, try something like this: