best way change template base my app

19 views
Skip to first unread message

carlos

unread,
May 13, 2015, 6:12:29 PM5/13/15
to django...@googlegroups.com
Hi, is posible change my base template with if tag
example:

{% if request.session.type = '1' %}
    {% extends "base.html"%}
{% else %}
   {% extends "base_other.html"%}
{%endif%}

i have a error like that

TemplateSyntaxError at /monito/cult/

Invalid block tag: 'endif'

will to do change the base template my app

Cheers

André Luiz

unread,
May 13, 2015, 7:41:45 PM5/13/15
to django...@googlegroups.com
extends should always be on first line of file and it accepts a variable as argument so you can define which template should be used on your view.

# views.py
def home(request):
    if request.session.type == 1:
         base = 'base.html'
    else:
        base = 'base_other.html'

    context = {
        'base': base,
    }

    return render(request, 'template.html', context)

# template.html
{% extends base %}

if you need it on every template maybe you should use a context processor to define your base variable.

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAM-7rO3DONtDe_d%2BHeBef446Mr%3DvSiKK7z_W8pyNyPq7Pbt3PA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

carlos

unread,
May 15, 2015, 3:50:40 AM5/15/15
to django...@googlegroups.com

Ilya Kazakevich

unread,
May 15, 2015, 2:39:34 PM5/15/15
to django...@googlegroups.com
You also may use widgets instead of "extends": http://sniplates.readthedocs.org/en/latest/
Reply all
Reply to author
Forward
0 new messages