[Django]-Allowing URL access only to logged-in users

7👍

To restrict views to only logged in users use django’s login required decorator

views.py

from django.contrib.auth.decorators import login_required

@login_required
def index(request):
    return render(request, 'professors/index.html')

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index')
]

If they are not logged in they will be redirected to your login page

👤Kareem

Leave a comment