简体   繁体   中英

@login_required decorator is not working (Django)

I am trying to build a social media like thing and have created the login and signup feature. For home page I have added login_required decorator and even added LOGIN_URL in settings.py but still I am able to access the home page without login ie login_required decorator is not doing its job. Here is my view function code

@login_required
def home(request):
return render(request, 'home/home.html')

Here is my code in settings.py

LOGIN_URL = '/login'

content of urls.py(account app)

urlpatterns = [
path('login/', views.login_view, name="login"),
path('create/', views.create_view, name="create",),
]

content of urls.py(home app)

urlpatterns = [
path('', views.welcome, name="welcome"),
path('home/', views.home, name="home"),
]

Can anyone find out what might be the problem?

  1. Make sure you have imported it: from django.contrib.auth.decorators import login_required

  2. Try to add: LOGIN_REDIRECT_URL

  3. Try: @login_required(login_url='login')

The problem was that I had still not created logout function. So the account was logged in and that's why login required was not working. It's simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM