Django:除主页面模板外,静态文件图像URL路径中断 [英] Django: static file image URL paths breaks except for main page template
问题描述
我正在开发中设置静态文件。我有一个图像位于polls / static / images / banner.jpg。当我导航到127.0.0.1:8000/横幅显示,但当我去像127.0.0.1:8000/2nd页面,横幅断开。
我的索引(URL:127.0.0.1:8000/)模板包含:
{%include'polls / header.html'%}
横幅的URL http://127.0.0.1:8000/static/images/banner.jpg
我的第2页模板还包含:
{%include'polls / header.html'%}
但横幅的网址更改为 http://127.0.0.1:8000/2ndpage/images/banner.jpg
我的polls / header.html模板:
< img src ={{STATIC_URL}} images / gcs_banner.jpg/>
urls.py
from django.conf.urls.defaults import patterns,include,url
urlpatterns = patterns('polls.views',
url(r'^ $' 'index'),
url(r'^ 2ndpage / $','2ndindex'))
views.py
def index(request):
...
return render_to_response 'polls / index.html',{'latest_people_list':latest_people_list,},context_instance = RequestContext(request))
def 2ndpage(request,people_id):
...
return render_to_response('index / detail.html',{'people':p},context_instance = RequestContext(request))
为什么URL从../static/ ..更改为../ 2ndpage / ..?我如何解决这个问题,所以当我使用{%include'polls / header.html'%}时,横幅总是显示?
我认为在第二页, {{STATIC_URL}}
未定义。因此,src以 images / gcs_banner.jpg
结束。这是一个相对网址,因为它不是前缀为斜杠。然后使用当前的绝对网址转换为绝对路径: /2ndpage/images/gcs_banner.jpg 。
{{STATIC_URL}}
可能是由上下文处理器设置的 - 至少它在我的项目中如何工作。 上下文处理器实际上是来自RequestContext 即可。当视图返回没有RequestContext的响应时,上下文处理器不会运行,例如:
从django导入快捷方式
#....
返回shortcuts.render_to_response(template_name,context)
这是显式使用RequestContext 与 render_to_response():
从django导入快捷方式
这就是说,Django 1.3提供了一个更好的快捷方式,它具有隐式使用RequestContext , render():
从django导入模板
#....
return shortcuts.render_to_response(template_name,context,
context_instance = template.RequestContext(request))
code>从django导入快捷方式
#....
return shortcuts.render(request,template_name,context)
I'm trying to setup my static file in development. I have an image located in polls/static/images/banner.jpg. When I navigate to 127.0.0.1:8000/ the banner shows up, but when I go to something like 127.0.0.1:8000/2ndpage the banner breaks.
my index (URL: 127.0.0.1:8000/) template contains:
{% include 'polls/header.html' %}
The URL for the banner http://127.0.0.1:8000/static/images/banner.jpg
my 2ndpage template also contains:
{% include 'polls/header.html' %}
But the URL for the banner changes to http://127.0.0.1:8000/2ndpage/images/banner.jpg
my polls/header.html template:
<img src="{{ STATIC_URL }}images/gcs_banner.jpg" />
urls.py
from django.conf.urls.defaults import patterns, include, url urlpatterns = patterns('polls.views', url(r'^$', 'index'), url(r'^2ndpage/$', '2ndindex'))
views.py
def index(request): ... return render_to_response('polls/index.html', {'latest_people_list': latest_people_list,}, context_instance = RequestContext(request)) def 2ndpage(request, people_id): ... return render_to_response('index/detail.html', {'people': p}, context_instance = RequestContext(request))
Why does the URL change from ../static/.. to ../2ndpage/..? How do I fix this so that when I use {% include 'polls/header.html' %} the banner always shows up?
解决方案I think that in the second page,
{{ STATIC_URL }}
is not defined. Thus, src ends up withimages/gcs_banner.jpg
. Which is a relative url because it's not prefixed with a slash. It is then converted to a absolute path using the current absolute url: /2ndpage/images/gcs_banner.jpg.
{{ STATIC_URL }}
is probably set by a context processor - at least that how it works in my projects. Context processors are actually a feature from RequestContext. When a view returns response without RequestContext then the context processors are not run, e.g.:from django import shortcuts # .... return shortcuts.render_to_response(template_name, context)
This is an example of explicit usage of RequestContext with render_to_response():
from django import shortcuts from django import template # .... return shortcuts.render_to_response(template_name, context, context_instance=template.RequestContext(request))
That said, Django 1.3 provides a better shortcut with implicit usage of RequestContext, render():
from django import shortcuts # .... return shortcuts.render(request, template_name, context)
这篇关于Django:除主页面模板外,静态文件图像URL路径中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!