Django login error, LOGIN_REDIRECT_URL is not working

95
May 30, 2021, at 09:00 AM

When I click the login button the next page shows HTTP ERROR 405.

Register works fine, but the only login doesn't work. Should I make login class and 'success_url = reverse_lazy('dashboard')'?

I think LOGIN_REDIRECT_URL is the problem, but I'm not sure. Why is this happening?

urls.py is

from django.contrib import admin
from django.shortcuts import redirect
from django.urls import path
from apps.common.views import HomeView, SignUpView, DashboardView
from django.contrib.auth import views as auth_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', HomeView.as_view(), name='home'),
path('dashboard/', DashboardView.as_view(), name='dashboard'),
path('register/', SignUpView.as_view(), name='register'),
path('login/', auth_views.LoginView.as_view(
template_name = 'common/login.html'
),
name='login'),
 path('logout/', auth_views.LogoutView.as_view(
    next_page='home'
    ),
    name='logout'
),
]

view.py is

    from django.shortcuts import render, redirect
    from django.http import HttpResponse
    from django.views.generic import TemplateView, CreateView
    from django.contrib.auth.mixins import LoginRequiredMixin
    from .forms import SignUpForm
    from django.urls import reverse_lazy
    from django.contrib.auth import authenticate, login
    from django.contrib import messages
    from django.contrib.auth.views import LoginView  
    class HomeView(TemplateView):
        template_name = 'common/home.html'
    class DashboardView(TemplateView):
       template_name = 'common/dashboard.html'
        login_url = reverse_lazy('home')
    class SignUpView(CreateView):
        form_class = SignUpForm
        success_url = reverse_lazy('home')
        template_name = 'common/register.html'

I also add LOGIN_REDIRECT_URL = 'dashboard' in settings.py

Answer 1

You should make a POST request to the SignUpView, not to the current view, the form thus looks like:

<form action="{% url 'register' %}" method="POST">
    <!-- … -->
</form>
READ ALSO
Notifiy my RecyclerView that data has changed

Notifiy my RecyclerView that data has changed

I am developing an shop app in android and got a question about transfering data between two adapterclassesThis is the scenario:

94
Add animation to React Slider

Add animation to React Slider

I have a very simple react sliderThere's an option to navigation between the slides

76
Mysql like query suggest best indexes for fast response

Mysql like query suggest best indexes for fast response

My query takes 90sec for return only 20 rowsQuery has some join and like query for searching keywords

54
SendGrid API is grouping tables together

SendGrid API is grouping tables together

When trying to send an email using sendgrid it is grouping tables togetherThis is how I want the email to look

29