I have created a user login and register. Now I want to redirect the home page to the login page when the user clicks “logout”.
I am not sure why but I keep having problems whenever I redirect.
This is my views.py
from django.contrib.auth import forms
from django.contrib.auth.models import User
from django.contrib.messages.api import error
from django.shortcuts import render, redirect, HttpResponse
from django.views.generic import View, TemplateView
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from .forms import CreateUserForm
def registerPage(request):
form = CreateUserForm()
if request.method == 'POST':
form = CreateUserForm(request.POST)
if form.is_valid():
form.save()
user = form.cleaned_data.get('username')
messages.success(request, 'Account was successfully created for '+ user)
# return redirect('login')
else:
messages.error(request, 'Unsuccessful! ')
context = {'form':form}
return render(request, 'home/register.html', context)
def loginPage(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
messages.error(request, 'Incorrect username or password')
context = {}
return render(request, 'home/login.html', context)
def logoutUser(request):
logout(request)
return redirect('login')
def home(request):
return render(request, 'home/home.html')
My app urls.py
from django.urls import path
from django.conf.urls import url
from . import views
app_name = 'user'
urlpatterns = [
path('register/', views.registerPage, name="register"),
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),
path('', views.home, name="home"),
url(r'^feedback/$', views.feedback, name="feedback"),
]
My project urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
urlpatterns = [
path('admin/', admin.site.urls),
path('sentiment/', include('sentiment.urls')),
path('', include('user.urls')),
]
My home.html
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Home page</title>
</head>
<body>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="/sentiment/type">Input Text</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/sentiment/import">Import Tweets</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/feedback">Feedback</a>
</li>
</ul>
</div>
<span ><a class="hello-msg" href="{% url 'logout' %}">Logout</a></span>
</nav>
<h1>Welcome To Homepage</h1>
Can anyone help me with this? I have searched everywhere but still couldn’t solve this issue. Let me know if you need anything else.