Enforce login required for page

From TRCCompSci - AQA Computer Science
Revision as of 11:00, 6 October 2019 by Admin (talk | contribs) (Created page with "=In the views.py= in the 'views.py' add the 'login_required' to the import: <syntaxhighlight lang=python> from django.contrib.auth.decorators import login_required </syntaxh...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

in the 'views.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 rap 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'),