Python Django静态URL不工作
Django版本是1.4。我读了Python Django静态URL不工作,python,django,web,django-staticfiles,Python,Django,Web,Django Staticfiles,Django版本是1.4。我读了官方文件,用谷歌搜索了我的问题 首先,我按照官方文件在settings.py中添加了以下内容: TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.debug', 'django.core.context_processors.i18n', 'django.core.context_processors.media', 'django.core.context_proces
官方文件
,用谷歌搜索了我的问题
首先,我按照官方文件在settings.py
中添加了以下内容:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
在我的模板中:
<link href="{{ STATIC_URL }}css/main.css" ...>
在我的视图中
def register(request):
...
return render_to_response('register.html', {'errors':errors})
你不需要在报税单上写上这个吗
context\u实例=RequestContext(请求)
更改
return render_to_response('register.html', 'errors':errors)
到
不幸的是,Django的
render_to_response
快捷方式默认使用普通模板上下文,其中不包括上下文处理器及其所有新奇有用的东西,如静态URL
。你需要使用,它确实做到了这一点
这可以通过使用新的(从Django 1.3开始提供)调用:
在Django 1.2及更早版本中,您需要显式提供上下文:
from django.shortcuts import render_to_response
from django.template import RequestContext
return render_to_response('register.html', {'errors':errors},
context_instance=RequestContext(request))
在Django 1.4中,应该使用
static
templatetag
尝试:
{%load staticfiles%}
我意识到这已经得到了回答,但我想在静态URL仍然呈现为空的情况下提供另一个答案,即使在使用RequestContext时也是如此
如果您正在运行开发服务器,请记住使用不安全标志启动服务器,让服务器为您的静态文件服务:
python manage.py runserver --insecure
你的应用程序中是否有文件夹static
?这是正确答案,但加载行是错误的,应该是:{%load staticfiles%}
@sberder,是的,你是对的<代码>{%load static%}
来自Django 1.3。我已经更改了答案。对我来说,我的settings.py
文件中没有设置TEMPLATE\u CONTEXT\u处理器。这个答案帮助我使它工作起来。
return render_to_response('register.html', {'errors': errors}, RequestContext(request))
from django.shortcuts import render
return render(request, 'register.html', {'errors':errors})
from django.shortcuts import render_to_response
from django.template import RequestContext
return render_to_response('register.html', {'errors':errors},
context_instance=RequestContext(request))
{% load staticfiles %}
<link href="{% static "css/main.css" %} ...>
python manage.py runserver --insecure