问题描述
我的 settings.py
包含以下配置参数。
My settings.py
contains the following configuration parameters.
STATIC_ROOT = ''
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
'C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows',
)
我的项目的CSS文件是位于 C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows/static/default.css
。
My project's CSS file is located at C:/Users/ABC/Desktop/DBMS/DjangoProject/tvshows/static/default.css
.
我有一个模拟HTML文件,应该拉CSS内容,但URL是一个404。
I have a mock HTML file that should pull in the CSS content, but the URL is a 404.
< link rel =样式表href ={{STATIC_URL}} static / default.css/>
我做错了什么?
What am I doing wrong?
推荐答案
需要检查的内容:
-
DEBUG = True
in settings.py
DEBUG = True
in settings.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf goes here ...
urlpatterns += staticfiles_urlpatterns()
使用或如果 {{STATIC_URL}}
不工作
作为一个简短的复习,上下文
处理器将变量添加到每个模板的
上下文中。然而,
上下文处理器要求
在渲染
模板时使用RequestContext。如果您使用通用视图,则会自动发生
,但是在
手动编写的视图中,您需要
显式地使用RequestContext才能看到
的工作原理,以及阅读更多
详细信息,查看子类
上下文:RequestContext。
As a brief refresher, context processors add variables into the contexts of every template. However, context processors require that you use RequestContext when rendering templates. This happens automatically if you're using a generic view, but in views written by hand you'll need to explicitally use RequestContext To see how that works, and to read more details, check out Subclassing Context: RequestContext.
这篇关于静态媒体无法在Django中加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!