Difference between revisions of "Enforce login required for page"

From TRCCompSci - 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...")
 
(In the urls.py)
Line 18: Line 18:
 
=In the urls.py=
 
=In the urls.py=
  
in the 'views.py' add the 'login_required' to the import:
+
Alternatively, in the 'views.py' add the 'login_required' to the import:
  
 
<syntaxhighlight lang=python>
 
<syntaxhighlight lang=python>

Revision as of 08:50, 13 February 2020

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