Difference between revisions of "Enforce login required for page"

From AQA Computer Science
Jump to: navigation, search
(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...")
 
(No difference)

Latest revision as of 10:00, 6 October 2019

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'),