开发者

change extends page in django

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 %}


It is possible to pass a variable to extends. From django book:

In most cases, the argument to {% extends %} will be a string, but it can also be a variable, if you don’t know the name of the parent template until runtime. This lets you do some cool, dynamic stuff.

However, do note that there is a caveat!

If you use {% extends %} in a template, it must be the first template tag in that template. Otherwise, template inheritance won’t work.

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:

if login: 
    sub_page = '/p_change/newpage.html'
else:
    sub_page = '/p_h/newpage1.html'

# 'base.html' being the base template containing {% extends page %}
return render_to_response('base.html', context_instance=RequestContext(request, {
    'page' : sub_page,
}))

Of course, I'm just speculating on what you're trying to achieve and may well be mistaken.


You can pass a variable to extends, if that's what you're asking.

{% extends page %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜