2

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 -->

1 Answers1

1

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

fakeMake
  • 738
  • 8
  • 18
  • thats worked prefectly, but now the problem is the urls are keeps on appending like this `http://127.0.0.1:8000/logout/login_page` –  Apr 24 '21 at 09:49
  • but i want url like this => for logout page= `http://127.0.0.1:8000/logout` for login page= `http://127.0.0.1:8000/login_page` –  Apr 24 '21 at 09:51
  • 1
    That is the only way. It has to be that way. – fakeMake Apr 24 '21 at 10:01
  • 1
    Accept as an answer if it solved your problem. – fakeMake Apr 24 '21 at 10:02
  • but what i do for urls.. i am beginner and dont know how to handle this.. –  Apr 24 '21 at 10:45
  • Now I figured it out myself by using reverse in the views.py file . reverse handled the appending urls very nicely and now the code is working as expected.. in b/w, thanks @fakeMake for helping out. –  Apr 24 '21 at 11:07