1

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 %}
GTS Joe
  • 3,612
  • 12
  • 52
  • 94

2 Answers2

2

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

Harrison
  • 188
  • 2
  • 9
0

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.

GTS Joe
  • 3,612
  • 12
  • 52
  • 94