如何在 django 开发服务器中提供静态文件
这个问题已经回答过好几次了,我几乎看过所有相关的帖子,但是 无法加载 css 文件。我的项目中有这样的结构: 我的网站 /模板 /设置.py /urls.py /myapp /静止的 /CSS /测试.css 在 settings.py 中我有这个代码:
import os
PROJECT_PATH=os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH,'static',)
STATIC_URL = '/static/'
在 urls.py 中我有这个代码:
from django.conf.urls.defaults import*
from mysite.myapp.views import test
urlpatterns = patterns('',(r'^home/$',test),)
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^static/(?P<path>.*)$','serve'),
)
我在模板中使用这个
{{STATIC_URL}}css/test.css
我做错了什么? 我应该做什么?比如应该添加任何东西 静态文件目录?或者在设置中的 INSTALLED_APPS 中? 请假设我是一个绝对的初学者。 我真的需要一个答案。 谢谢。
this question has been answered several times and i have seen almost all related posts,but
couldn't get css files load. i have this structure in my project:
mysite
/templates
/settings.py
/urls.py
/myapp
/static
/css
/test.css
in settings.py i have this code:
import os
PROJECT_PATH=os.path.realpath(os.path.dirname(__file__))
STATIC_ROOT = os.path.join(PROJECT_PATH,'static',)
STATIC_URL = '/static/'
in urls.py i have this code:
from django.conf.urls.defaults import*
from mysite.myapp.views import test
urlpatterns = patterns('',(r'^home/
i use this in the template
{{STATIC_URL}}css/test.css
what wrong am i doing?
is there anything should i be doing?like should add anything in
STATICFILES_DIRS? or in INSTALLED_APPS in settings?
kindly assume that i am an absolute beginner.
i really need an answer.
thank you.
,test),)
from django.conf import settings
if settings.DEBUG:
urlpatterns += patterns('django.contrib.staticfiles.views',
url(r'^static/(?P<path>.*)
i use this in the template
what wrong am i doing?
is there anything should i be doing?like should add anything in
STATICFILES_DIRS? or in INSTALLED_APPS in settings?
kindly assume that i am an absolute beginner.
i really need an answer.
thank you.
,'serve'),
)
i use this in the template
what wrong am i doing?
is there anything should i be doing?like should add anything in
STATICFILES_DIRS? or in INSTALLED_APPS in settings?
kindly assume that i am an absolute beginner.
i really need an answer.
thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您仍然需要将
STATIC_URL
传递给视图中的模板。最简单的方法是使用RequestContext
,只要“TEMPLATE_CONTEXT_PROCESSORS”包含static
条目即可。you still need to pass
STATIC_URL
to the template in your view. The easiest way to do this is usingRequestContext
as long as the 'TEMPLATE_CONTEXT_PROCESSORS' include thestatic
entry.