Monday, January 2, 2023

Restrict a page to view only on login

 By using the annotation @login_required


If we don't pass any parameter, when user tried to access, it will give 404 error.

So, passing the redirection url also to it if not logged in.



#views.py
#---
from django.shortcuts import render
from django.contrib.auth.decorators import login_required

@login_required(login_url="/admin")  ##to redirect to admin page instead of 404 error with just @login_required
def authorized(request):
  return render(request,'home/authorized.html',{})


#urls.py
#---
urlpatterns=[
path('authorized',views.authorized),
]

No comments:

Post a Comment