{% extends base_template|default:"base.html" %}
This sounds like it isn't applicable in your case though since it sounds like you want the same logic on every page.
An addition to Tom's suggestion is making your own context manager to automatically insert the variable into all template contexts.
In some context_processors.py file somewhere in your project:
def base_template(request):
if not request.user.is_active:
return {"base_template": "base_noclient.html"}
if not request.user.is_staff:
return {"base_template": "base_client.html"}
return "base.html"
# I changed the ordering of your logic just because my tendency is to put the most common cases towards the top.
After you have that file you need to add it to your settings.py:
In TEMPLATE_CONTEXT_PROCESSORS = (
....
"project.app.path.context_processors.base_template", # make sure this is the actual path to the file.
)
Finally, make sure you use RequestContext instead of just context. The easiest way is to use `render` instead of render_to_response:
from django.shortcuts import render
def some_view(request):
return render(request, "path/to/template.html", {"view_context": ...}) # The "render(request" portion will automatically send the base_template variable into your template.
On Wednesday, March 6, 2013 2:43:10 AM UTC-8, Tom Evans wrote:
-- On Tue, Mar 5, 2013 at 9:06 PM, Sandeep kaur <mkaur...@gmail.com> wrote:
> I want to extend the base template file into the child file depending
> on the condition, like if the user is superuser or staff or active
> user, the file to extended should ll be different.
> For Eg :
>
> {% if user.is_superuser %}
> {% extends "base.html" %}
> {% else if user.is_active %}
> {% extends "base_client.html" %}
> {% else %}
> {% extends "base_noclient.html" %}
>
>
> However this code doesn't work.
> What should be done. Your help will be highly appreciated.
> Thank you.
>
{% extends %} can either take a string literal, or a variable. Move
the logic to decide which base template to use into your view, pass
the variable to the template in the context, and use that as the
argument to {% extends %}.
Cheers
Tom
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment