petros
petros

Reputation: 715

Django template tags on base.html

I have a base page and some templates with views:

<!DOCTYPE html>
<head>
    <title>{% block title %}title{% endblock %}</title>
</head>

<body>
   <div id="userpanel"> 
      user panel
   </div>
   <div id="content">
        {% block content %}{% endblock %}
    </div>
</body>
</html>

and

{% extends "base.html" %}

{% block title %}some title{% endblock %}

{% block content %}
some content
{% endblock %}

I want to add user panel on base page. Can I somehow use there template tags like if/for? I would like to be able to use something like:

{% if user.admin}
admin panel
{% endif %}

{% if user.moderator}
moderator panel
{% endif %}

Upvotes: 1

Views: 1463

Answers (1)

rollinger
rollinger

Reputation: 541

The context variable always has the request.user (the current user).

So you can do: if request.user.is_superuser ... And: If request.user.is_staff ....

Both vars you can assign in the Admin.auth.user.

Your second question regarding to make an assignment tag, see: here

# app/templatetags/mytags.py
@register.assignment_tag
def get_messages_list_for_user(user):
    list = Messages.objects.filter(owner=user)
    return list

Then in the template you go:

# base.html
{% load mytags %}
{% for m in get_messages_list_for_user request.user %}
{{ m.text }}
{% endfor %}

You always have the request user so you can put it in one template or in your base template.

Upvotes: 3

Related Questions