Issue
I have a website and I want to prevent visitors to seeing content unless they have permission. How can I restrict them?
Solution
Restrict access to logged in users in Function based views
If you’re using function based views you can simply restrict all access to the view to users who are logged in, by decorating the function with the @login_required
decorator.
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
return HttpResponse()
The result of this will be that any user who is not logged in and who tries to access the view by its URL will be redirected to the login page of your website. Note that this decorator does not check if the user is active or not (using the is_active property), it only checks if the user is logged in or not.
Answered By - namjoo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.