Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我无法从孩子内部设置 django 模板变量,有什么想法吗?
在我的views.py中:
return { 'header_title' : 'my text' }
base.html:
{{ header_title }}
主.html:
{{ extends "base.html" }}
详细信息.html:
{{ extends "main.html" }}
那是行不通的。帮助?
您不能只从视图中返回字典。视图必须返回一个HttpResponse对象。请尝试:
HttpResponse
Django 1.3+渲染
return render(request, 'main.html', { 'header_title': 'my text' })
Django <1.3渲染响应
return render_to_response('main.html', { 'header_title': 'my_text' }, context_instance=RequestContext(request))
一个解决方案可能是放入你的 base.html
<block title><endblock>
在儿童模板中:
<block title>{{ header title}}<endblock>