Django静态文件CSS [英] Django Static Files CSS
问题描述
如何查看我的静态CSS文件?我已经设置了STATIC_ROOT,并且正在使用 python manage.py runserver .
How can I view my static css files? I've set my STATIC_ROOT, and am using python manage.py runserver.
在我的开发环境中,根据文档,我只需要将我的静态文件(在这种情况下为/static/css/typography.css)放在STATIC_ROOT中,然后 python manage.py runserver 会自动创建如果我有 DEBUG = True
,则需要访问它的视图.
In my development environment, according the docs, I only need to place my static files (in this case, /static/css/typography.css) in my STATIC_ROOT, and python manage.py runserver will automatic create the views necessary to access it if I have DEBUG = True
.
STATIC_ROOT = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static")
我还尝试了在URLConf中手动添加视图,该视图也不会显示css文件:
I've also tried manually adding the views in URLConf, which won't display the css file either:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
在我的模板中, {{STATIC_URL}}
到达正确的地址(/static/css/typography.css),但是当我尝试访问该文件时,它将不会提供文件:
In my template, the {{ STATIC_URL }}
gets to the correct address (/static/css/typography.css), but it will not serve the file when I try to access it:
<link href="{{ STATIC_URL }}css/typography.css" rel="stylesheet" type="text/css">
注意:关于StackOverflow的其他与Django相关的静态文件问题已有两年多的历史了.Django 1.3b1版区分STATIC(静态文件,例如CSS和图像)和MEDIA(用户上传的文件).
Notes: The other Django related static files questions on StackOverflow are over two years old. Django version 1.3b1 differentiates STATIC (static files, such as css and images) and MEDIA (user-uploaded file).
推荐答案
在这里回答: Django staticfiles应用程序帮助
我将文件放在STATIC_ROOT中,因此添加了它:
I was putting my files in STATIC_ROOT, so adding this works:
STATICFILES_DIRS = (STATIC_ROOT,)
这篇关于Django静态文件CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!