Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django静态URL不工作_Python_Django_Web_Django Staticfiles - Fatal编程技术网

Python Django静态URL不工作

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

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_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