Difference between revisions of "Enforce login required for page"
(→In the urls.py) |
(→In the urls.py) |
||
(One intermediate revision by the same user not shown) | |||
Line 18: | Line 18: | ||
=In the urls.py= | =In the urls.py= | ||
− | Alternatively, in the ' | + | Alternatively, in the 'urls.py' add the 'login_required' to the import: |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
Line 24: | Line 24: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Now in the 'urlpatterns' you should be able to | + | Now in the 'urlpatterns' you should be able to wrap the view to run in a 'login_required' for example this: |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> |
Latest revision as of 08:55, 25 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 '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'),