python / beginner
Snippet
Restricting Access with login_required
The login_required decorator is a quick way to ensure that only authenticated users can access a specific view function.
snippet.py
1
2
3
4
5
from django.contrib.auth.decorators import login_required@login_requireddef profile_view(request):return render(request, 'profile.html')
django
Breakdown
1
from django.contrib.auth.decorators import login_required
Imports the necessary decorator from Django's authentication system.
2
@login_required
A decorator that checks if the user is logged in before executing the function.
3
def profile_view(request):
The view function that is now protected and only accessible to members.