Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Showing different templates to admins

In Django what's the best way to implement templates with extra functionality for users with 'admin' permissions.

I'm not sure if I should create a set of completely different views specific for admins or integrate it into my existing views and templates like 'if user is an admin' everywhere.

Is there a standard way to do this in Django?

like image 259
9-bits Avatar asked Feb 25 '12 20:02

9-bits


People also ask

How do I change the admin template in Django?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

Can we customize Django admin template?

Customizing the Django Admin ModelAdmin . Most of the customization you can do with the Django admin is done by modifying ModelAdmin , and you sure can modify it!

Where are Django admin templates stored?

The default templates used by the Django admin are located under the /django/contrib/admin/templates/ directory of your Django installation inside your operating system's or virtual env Python environment (e.g. <virtual_env_directory>/lib/python3. 5/site-packages/django/contrib/admin/templates/ ).

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.


1 Answers

This will show the stuff only if you are active and staff not admin:

{% if request.user.is_active and request.user.is_staff %}
    {% include "foo/bar.html" %}
{% endif %}

If you wanna show only and ONLY for admin you have to do that:

{% if request.user.is_superuser %}
    ADD your admin stuff there.
{% endif %}

Differences about these fields here.

like image 182
Guillaume Avatar answered Sep 18 '22 23:09

Guillaume