更改 Django 中的扩展页面
我们可以将页面名称传递给 django 中的扩展标签吗
,即我想做类似
EDIT
if(login):
return render_to_response('/p_change/',
context_instance=RequestContext(request,{'page':'newpage.html'}))
else:
return render_to_response('/p_h/',
context_instance=RequestContext 的操作(请求,{'page':'newpage1.html'}))
{% extends page %}
Can we pass a page name to a extends tag in django
i.e, i want to do something like
EDIT
if(login):
return render_to_response('/p_change/',
context_instance=RequestContext(request,{'page':'newpage.html'}))
else:
return render_to_response('/p_h/',
context_instance=RequestContext(request,{'page':'newpage1.html'}))
{% extends page %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以将变量传递给
extends
。来自 django 书籍:但是,请注意,有一个警告!
这意味着您必须将页面选择逻辑放在视图中(而不是放在模板中,因为它将出现在
extend
标记之前,从而使模板继承无效)并在上下文中传递变量。有关更多详细信息,请参阅此答案。更新
在您更新的问题中,您的代码几乎就在那里,除了 render_to_response 应该是模板文件名,而不是目录。
我会将您的内容重写为:
当然,我只是猜测您想要实现的目标,并且很可能是错误的。
It is possible to pass a variable to
extends
. From django book:However, do note that there is a caveat!
This means you'll have to place the page selection logic in your views (and not in your template as that will come before the
extend
tag hence invalidating template inheritance) and pass in the variable in your context. See this answer for more details.Update
In your updated question, your code is almost there, except that the first argument to render_to_response should be an template filename, not a directory.
I would rewrite what you have as:
Of course, I'm just speculating on what you're trying to achieve and may well be mistaken.
如果您需要的话,您可以将变量传递给extends。
You can pass a variable to
extends
, if that's what you're asking.