B
B
BarneyGumble2019-12-17 21:51:45
Django
BarneyGumble, 2019-12-17 21:51:45

How to check for admin in Django template?

I inherited a working site-catalog on Django. Having previously only worked with PHP on the backend, I ran into one problem regarding checking admin rights in the template (although they say that everything worked before ... as usual) I have the following in the index.html
template :

{% if is_admin %}
    <a href="#" class="edit-button">Edit</a>
{% endif %}

The condition does not work, the button does not appear, although I am logged in as an admin in the admin panel. In the models.py
file where all models are described, there is a connection string for this library . However, this library is not used in the code below in any class. At least, I didn't find where and whether I should at all :) Tell me where to dig?) is_admin is some kind of basic check function, or a custom one, written somewhere. If so, where can I find a description of it?
from django.contrib.auth.models import User, Group

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
antonksa, 2019-12-17
@antonksa

Let's go in order.
1. Django template engine. It accepts html as input (in fact, not only, but any text file) and a box with New Year's gifts (no) variables, the so-called context. When processing a template, the template engine replaces the special syntax {{ variable_name }} that it looks for in the context (the context is a normal key-value dictionary).
2. User. A user is a built-in user model in Django. You can replace it with your own, but 90% use this one. The authorization middleware receives the request object before it is passed to the View for processing. In 90% of cases, the built-in middleware from the django.contrib.auth module is again used. The middleware is poking around in cookies, finds the session_key and searches the database (in 90% of cases, you get it) which user this session corresponds to. After that, the property of the HttpRequest instance is added/modified. This attribute is assigned a User instance picked up from the database or, if the user is not authorized, AnonymousUser.
3. View calls the render function which takes a template and a context. to collect the context, the View class has a get_context method that collects the context into a heap. Even if you just wrote the most banal

class MyFuckingView(TemplateView):
    template_name = 'some/module/template.html'

then when processing the get request, get_context from the parent TemplateView class will be called.
4. By default, janga pushes request and some other things into the context (the view instance that was used and, depending on the CBV (class based view) used, there may be a queryset, an object, and other things. Once again, this is a built-in functionality, so if you're modifying the context, then you always need to call the parent's method so that it doesn't get lost:
class MyFuckingView(TemplateView):
    template_name = 'some/module/template.html'

    def get_context(self):
        ctx = super().get_context()
        ctx['model_name'] = 'Sasha Grey'
        ctx['category'] = 'Milf anal'
        return ctx

5. Finally, in your case, the context should be request. is_admin could only appear there forcibly. Modify the template:
{% if request.user.is_admin %}
    <a href="#" class="edit-button">Edit</a>
{% endif %}

and everything should work. And re-read what I wrote above - the django tutorial can be mastered in a day or two, it is very simple on the one hand, and on the other, a framework thought out and tortured over 13 years of existence by the entire community.

W
WhiteJamer, 2019-12-17
@Whitejamer

{% if user.is_superuser %}
    # Hello, admin.
{% endif %}

If explained, then user is a request.user context variable that is created automatically.
Source: https://stackoverflow.com/questions/11916297/djang...

D
Dr. Bacon, 2019-12-17
@bacon

A context variable that is more likely to be defined somewhere in views.py, less likely to be defined in the context processor.
PS: without knowledge of the framework, I would not recommend getting into this code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question