在Django模板{%block%}标签中使用变量 [英] Use of variables in Django template {% block %} tags
问题描述
我有一个类似这样的基本模板
...
{%块标题%}< h1> Whoo< / h1> {%endblock%}
{%block Content%}< p>您好,< / p> {%endblock%}
...
此类扩展模板
{%extendsbase.html% }
...
{%block myblock.name%}< p> {{myblock.content}}< / p> {%endblock%}
< p> {{myblock.name}}< / br> {{myblock.content}}< / p>
...
并呈现为
myblock = {'name':'Title','content':'stuff'}
return render_to_response('extension.html',{'myblock' :myblock},context_instance = RequestContext(request))
我希望得到,并得到第一个项目:
...
< p>东西< p>
< p>您好,< / p>
< p>标题< / br>东西< / p>
...
但是在第二个项目中,我得到
...
< h1> Whoo< / h1>
< p>您好,< / p>
< p>标题< / br>东西< / p>
...
所以在第二个项目中,myblock dict被传递并处理模板,但似乎{%block myblock.name%}中的myblock.name被解释为文字而不是变量。关于如何强制Django评估{%block%}标签中的变量的任何想法?
你应该再看一下有关模板继承的文档。
...块标签定义[...]块模板可以填充
。所有的块标签都是告诉一个孩子
模板可以覆盖模板的那些部分的模板引擎。
但是您不要将变量分配给一个块,直接在您正在尝试的视图中。
和 {%block myblock.name%}
看起来很奇怪。
要收到结果,您期望我会说模板应该看起来像这样
{%扩展base.html%}
假设您正在使用最新版本的Django,您甚至可以使用
{%块标题%}< p> {{myblock.content}}< / p> {%endblock%}
render
您的快捷方式查看:return render(request,'extension.html',{'name':'Title','content' 'stuff'})
哪些会导致一个这样的模板:
{%extendsbase.html%}
{%block Title%}< p> {{content}}< / p> ; {%endblock%}
The main question is how can I programatically choose what block to put some content in. The following is working in a different project, but in a fresh project this does not work for some reason. I'm using the same (default) template context processors on the same machine for both projects.
I have a base template that goes something like this
... {% block Title %}<h1>Whoo</h1>{% endblock %} {% block Content %}<p>Hi there</p>{% endblock %} ...
And an extending template like this
{% extends "base.html" %} ... {% block myblock.name %} <p> {{ myblock.content }} </p> {% endblock %} <p> {{ myblock.name }} </br> {{ myblock.content }} </p> ...
And rendering as such
myblock = { 'name': 'Title', 'content': 'stuff' } return render_to_response( 'extended.html', {'myblock': myblock}, context_instance=RequestContext(request) )
I expect to get, and get on the first project:
... <p> stuff <p> <p>Hi there</p> <p> Title </br> stuff </p> ...
But on the second project I get
... <h1>Whoo</h1> <p>Hi there</p> <p> Title </br> stuff </p> ...
So on the second project, the myblock dict is passed and processed by the template but it seems that the myblock.name in {% block myblock.name %} is interpreted as a literal and not a variable. Any ideas on how to force Django to evaluate a variable inside a {% block %} tag?
解决方案You should take another look at the documentation about template inheritance.
... the block tag defines [...] blocks that child templates can fill in. All the block tag does is to tell the template engine that a child template may override those portions of the template.
But you don't assign a variable to a block directly in a view as you are trying to do.
And
{% block myblock.name %}
looks strange too.To receive the result you are expecting I'd say the template should rather look like this
{% extends "base.html" %} {% block Title %}<p>{{ myblock.content }}</p>{% endblock %}
Assuming you are using a recent version of Django you could even simplify things using the
render
shortcut in your view:return render(request, 'extended.html', {'name': 'Title', 'content': 'stuff'})
Which would lead to a template like this:
{% extends "base.html" %} {% block Title %}<p>{{ content }}</p>{% endblock %}
这篇关于在Django模板{%block%}标签中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!