login_required not working django even after cache clear

1.3k views Asked by At

I want the user not to navigate back once he is logged out. I used login_required(login_url = "/login/") before my definition of logout view in my views.py. I am still able to navigate back after logging out. How can I ask the user to log in again? Also, If I type URL for my home page (say localhost:8000/home) directly in the browser, I am able to access the page without login. How can I correct this issue?

I have cleared browser cache too and still, no use.

my views.py:

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.views.decorators import csrf
from django.contrib.auth.decorators import login_required

def login(request):
    c={}
    # c.update(csrf(request))
    return render(request,'login.html',c)


def auth_view(request):
    username = request.POST.get('username','')
    password = request.POST.get('password','')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/home')

@login_required(login_url='/login/')
def logout(request):
    request.session.flush()
    auth.logout(request)
    return render(request,'logout.html')
1

There are 1 answers

3
Tevin Joseph K O On BEST ANSWER

If you want to protect your home page or other pages with login, you should decorate them with login_required decorator. For example,

@login_required(login_url='/login/')
def home(request):
    # Your code goes here