django 2.13 login_required build-in decorator doesn't work

246 Views Asked by At

i work on Django 2.1.2 and i wanted decorated my view base on class. I apply login_required decorator in path

path('', login_required(CredentialsList.as_view()), name='credentials-list'),

when i send request to CredentialList it responds normally, it does not redirect me to the login screen. whether I omitted something from the configuration

LOGIN_URL='login/'
LOGIN_REDIRECT_URL = 'list/'
2

There are 2 best solutions below

1
On

Instead you can use the LoginRequiredMixin or the decorator in your view. For example:

# views.py
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required

# For a class-based view
class CredentialsList(LoginRequiredMixin, TemplateView):

# For a function-based view
@login_required
def credentials_list(request):

and for your url remove the decorator:

path('', CredentialsList.as_view(), name='credentials-list'),
0
On

I run this project on another linux (ubuntu 16.04), where is another enviroment of course and everything work fine :|. I must check in home what was wrong, maybe must create another virualenv. Thank you all for your help me!!!