Search code examples
djangodjango-viewsdjango-formsdjango-authenticationdjango-auth-models

Logout function in Django not working as Expected


I have not used any middleware, when i click on logout button on my Home Page template, the logout function execute without any error.but when i go back to main page without jumping to login page.. i see myself as logged in user

here is my authentiCation/views.py

from django.shortcuts import render
from django.http import request,HttpResponseRedirect
# for user creation & login form
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login
# for user related Queries
from django.contrib.auth.models import User


# imports for test purpose
from django.http import HttpResponse

# Create your views here.

# register page 
def register_Page(request):
    if request.method == 'POST':
        form= UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username= request.POST['username']
            password= request.POST['password1']
            user= authenticate(request,username=username,password=password)
            login(request,user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('Either the user name is not available or you may have filled the form incorrectly')
    else:
        form = UserCreationForm()
        context= {'form':form}
        return render(request,'authentication/register_Page.html',context)

# login page
def login_page(request):
    if request.method == 'POST':
        username= request.POST['username']
        password= request.POST['password']
        # returns user if credentials are valid
        user= authenticate(request, username=username, password= password)
        # check if user var contains the user
        if user is not None:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return HttpResponse('Invalid credentials')

    return render(request,'authentication/login.html')

# logout Page
def log_out(request):
    logout(request)
    return HttpResponseRedirect('logout_page')

authentiCation/urls.py

from django.urls import path
from authentiCation import views

urlpatterns = [
    path('register/',views.register_Page),
    path('login/',views.login_page,name='login_page'),
    path('logout/',views.log_out),
]

Main App Files urls

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('App_wfi_Community.urls')),
    path('Ask/',include('askQuestion.urls')),
    path('auth/',include('authentiCation.urls')),
]

Home.html

{% extends "basic.html" %}
{% load humanize %}
{% block title %}WFI-Community{% endblock title %}
{% block body %}

<!--  search button   -->
<h5>Welcome {{username}},<h5> <!--  I see my username here   -->
<form action="/search" method="get">
<div class="container py-3 row">
  <div class="col-md-8 offset-2">
    <div class="input-group">
      <input name="searchfieldText" type="text" class="form-control" placeholder="Search">
      <button class="btn btn-danger" type="submit">Search</button>
      <span><a href="{% url 'login_page' %}">Logout</a></span>
    </div>
  </div>
</div>
</form>
<!--  and some more HTML stuff which is irrelavent here -->

Solution

  • Give a name to this eg path('logout/',views.log_out, name="logout" ) and change it in the home.html. eg "{% url 'logout' %}">Logout