Enforce login required for page

From TRCCompSci - AQA Computer Science
Jump to: navigation, search

In the views.py

in the 'views.py' add the 'login_required' to the import:

from django.contrib.auth.decorators import login_required

Now you should be able to add '@login_required' before the def of any view which should be restricted, for example:

@login_required
def update(request):

This will mean that in order to access this page the user will need to be already logged in.

In the urls.py

Alternatively, in the 'urls.py' add the 'login_required' to the import:

from django.contrib.auth.decorators import login_required

Now in the 'urlpatterns' you should be able to wrap the view to run in a 'login_required' for example this:

path('update', user_views.update, name='update'),

Will become this:

path('update', login_required(user_views.update), name='update'),