Python Forum
Protected Pages with Django - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Protected Pages with Django (/thread-16047.html)



Protected Pages with Django - xxp2 - Feb-12-2019

Hi everyone .. I wanna make a simple project with Django. I need to safe page project.
For example, if somebody doesn't logging and he/she gets to request a page then return 403 status code.

How I can do that. Thank u for the answer:)


RE: Protected Pages with Django - nilamo - Feb-12-2019

The @login_required decorator: https://docs.djangoproject.com/en/2.1/topics/auth/default/#django.contrib.auth.decorators.login_required
Quote:
from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):

Or, if you want to check for certain group membership (so only some members can get in, instead of just anyone logged in), you can use @user_passes_test https://docs.djangoproject.com/en/2.1/topics/auth/default/#limiting-access-to-logged-in-users-that-pass-a-test
Quote:
from django.contrib.auth.decorators import user_passes_test

def email_check(user):
    return user.email.endswith('@example.com')

@user_passes_test(email_check)
def my_view(request):



RE: Protected Pages with Django - xxp2 - Feb-12-2019

(Feb-12-2019, 06:34 PM)nilamo Wrote: The @login_required decorator: https://docs.djangoproject.com/en/2.1/topics/auth/default/#django.contrib.auth.decorators.login_required
Quote:
from django.contrib.auth.decorators import login_required @login_required def my_view(request):
Or, if you want to check for certain group membership (so only some members can get in, instead of just anyone logged in), you can use @user_passes_test https://docs.djangoproject.com/en/2.1/topics/auth/default/#limiting-access-to-logged-in-users-that-pass-a-test
Quote:
from django.contrib.auth.decorators import user_passes_test def email_check(user): return user.email.endswith('@example.com') @user_passes_test(email_check) def my_view(request):



thank you