简体   繁体   中英

Django: Can't redirect to home page after login

I'm developing simple user authentication with a gorgeous frontend. It has only one app 'account'. under the app I created a 'registerPage' view and the register process working fine. But for login I'm using default view from django.contrib.auth. --- Problem is, after giving the email & password to login it should be redirect to home page, but remains in the same login page

account/views:

from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required
# views here...
from .models import *
from .forms import CreateUserForm


def registerPage(request):        
    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 created for ' + user)
            return redirect('login')
    else:
        form = CreateUserForm()
    context = {'form':form}
    return render(request, 'account/register.html', context)

def logoutPage(request):
    logout(request)
    return redirect('login')

@login_required
def homePage(request):
    context = {}
    return render(request, 'account/home.html', context)

account/urls:

from django.urls import path
from . import views


urlpatterns = [
    path('', views.homePage, name="home"),
    path('register/', views.registerPage, name="register"),
    # path('login/', views.loginPage, name="login"),  
]

login.html: main body

<main class="login-body" data-vide-bg="{% static 'img/login-bg.mp4' %}">
        <!-- Login Admin -->
        <form class="form-signin" action="" method="POST">
            {% csrf_token %}
            <div class="login-form">
                <!-- logo-login -->
                <div class="logo-login">
                    <a href="{% url 'home' %}"><img src="{% static 'img/logo/loder.png' %}" alt=""></a>
                </div>
                {% for message in messages %}
                    <div class="alert alert-{{ message.tags }}">
                        {{ message }}
                    </div>
                {% endfor %}

                <h2>Login Here</h2>
                <div class="form-input">
                    <label for="name">Email</label>
                    <input  type="email" name="email" placeholder="Email">
                </div>
                <div class="form-input">
                    <label for="name">Password</label>
                    <input type="password" name="password" placeholder="Password">
                </div>

                <!-- {% if request.GET.next %}
                    <input type="hidden" name="next" value="{{ request.GET.next }}">
                {% endif %} -->
                <div class="form-input pt-30">
                    <input type="submit" name="submit" value="login">
                </div>

                Don't have an account ? <a href="{% url 'register' %}" class="register"> Sign-up </a>
            </div>
        </form>
        <!-- /end login form -->

    </main>

project urls:

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views
from account import views as acc_views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('register/', acc_views.registerPage, name='register'),
    path('login/', auth_views.LoginView.as_view(template_name='account/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(template_name='account/logout.html'), name='logout'),
    path('', include('account.urls')),
]

I also added those route in settings.py file:

LOGIN_REDIRECT_URL = 'home'

LOGIN_URL = 'login'

Edit LOGIN_REDIRECT_URL = 'home' -> LOGIN_REDIRECT_URL = '/'

It should work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM