'restricted url access Django
I built myself the auth system of the website, and I created later on a Django app 'homepage' that can only be accessed when the user is logged so I have to create a way to restrict users from typing the app name into the url I searched on google and found the login_required function but I think this applies only If I used django auth system, and if that's not the case, how django can know if a user is logged in based on my auth system that I created it
Urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('loginpage.urls')),
path('signup/',include('signup.urls')),
path('homepage/',include('Homepage.urls'))
]
loginpage.views
def login_page_fetch(request):
return render(request,'loginpage/login.html')
def check_user(request):
if request.method=='POST':
email_client=request.POST.get('email')
password_client=request.POST.get('psw')
try:
user=User.empAuth_objects.get(email_address=email_client,password=password_client)
if user is not None:
return HttpResponseRedirect(reverse('Homepage:home_page_fetch'))
except User.DoesNotExist:
return render(request,'loginpage/login.html',{'l':['error']})
else:
raise Http404('error')
loginpage.urls:
app_name='loginpage'
urlpatterns = [
path('',views.login_page_fetch,name='login_page_fetch'),
path('Verification/',views.check_user,name='check_user')
]
homepage.urls
app_name='Homepage'
urlpatterns = [
path('',views.home_page_fetch,name='home_page_fetch'),
path('Hotels/',views.hotels_generator,name='hotels_generator'),
path('Book_now/<name>/<id_hotel>/<add>',views.book_now,name='book_now')
]
homepage.views
def home_page_fetch(request):
return render(request,'Homepage/homepage.html')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
