Home > Software engineering >  Login Button not redirecting back to home page
Login Button not redirecting back to home page

Time:10-29

I am not getting redirected back to the homepage for some reason. I checked the creds to make sure it was correct and it was. I am not sure what's going on. I am not sure if I have to make it a button but I did the same thing before and it worked.

urls.py

from django.urls import path
from .views import pplCreate, pplCreate, pplList, pplDetail,pplUpdate,pplDelete,authView

urlpatterns = [
    path('login/', authView.as_view(),name='login'),
    path('', pplList.as_view(), name='pplLst'),
    path('people/<int:pk>', pplDetail.as_view(), name='pplDet'),
    path('people-update/<int:pk>', pplUpdate.as_view(), name='pplUpd'),
    path('people-create/', pplCreate.as_view(), name='pplCre'),
    path('people-delete/<int:pk>', pplDelete.as_view(), name='pplDel'),

]

views.py

from distutils.log import Log
import imp
from .models import People
from django.shortcuts import  render
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.urls import reverse_lazy

from django.contrib.auth.views import LoginView

# Create your views here.

class authView(LoginView):
    template_name = 'base/login.html'
    fields = '__all__'
    redirect_authenticated_user = True

    def get_success_url(self):
        return reverse_lazy('pplLst')

class pplList(ListView):
    model = People
    context_object_name = 'people'

class pplDetail(DetailView):
    model = People
    context_object_name ='cnd'
    template_name = 'base/people.html'

class pplCreate(CreateView):
    model = People 
    fields = '__all__'
    success_url = reverse_lazy('pplLst')

class pplUpdate(UpdateView):
    model = People 
    fields = '__all__'
    success_url = reverse_lazy('pplLst')

class pplDelete(DeleteView):
    model = People 
    context_object_name = 'cnd'
    success_url = reverse_lazy('pplLst')

login.html

<h1>Login</h1>

<from method="POST">
    {%csrf_token %}
    {{form.as_p}}
    <input type="submit" value="Login">
</from>

CodePudding user response:

In settings.py file, simply you can add this.

LOGIN_REDIRECT_URL = 'pplLst'  #This is path name from urls

Add this in settings.py file, then you will get redirected to home page

Just remove auth view from views and do following way:

In urls.py:

from django.contrib.auth import views as auth_views

path('login/',auth_views.LoginView.as_view(template_name='base/login.html'),name='login'),
  • Related