Home > Back-end >  Django Show Code in Template if Logged In
Django Show Code in Template if Logged In

Time:11-03

In a Laravel Blade template, I can do this to show code only if a user is logged in:

@auth
<div>Show code only if logged in.</div>
@endauth

Does Django have a template tag or something similar that does the equivalent? E.g.:

{% auth %}
<div>Show code only if logged in.</div>
{% endauth %}

CodePudding user response:

In Django templates you should check for {% if user.is_authenticated %}. See this answer for more.

CodePudding user response:

I'll answer my own question since I found more of what I was looking. In Django, they're called "decorators," e.g., @login_required.

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

Source: https://docs.djangoproject.com/en/3.2/topics/auth/default/#the-login-required-decorator

Added: 11/02/2021.

  • Related