簡體   English   中英

登錄失敗頁面后的 Django 未重定向到下一個

[英]Django after login failed page is not redirecting to next

如果用戶成功登錄,則它會重定向到下一頁,但如果用戶無法登錄,那么我想更改模板並希望重定向到下一頁,但它不起作用。

視圖.py

def login(request, template_path='login.html'):
"""
"""
# We'll only allow staff to view this page after login.
if request.user.is_authenticated:
    return redirect('home')

if request.method == 'POST':

 if request.META['HTTP_REFERER'].split('/')[-1] == 'bs5':
        template_path = 'theme/bs5_theme.html'

    response = LoginView.as_view(
        template_name=template_path,
        redirect_field_name='next',
        form_class=EmailAuthenticationForm,
    )(request)

    if request.is_ajax():
        if request.user.is_authenticated:
            return HttpResponse(
                json.dumps(dict(success=True, next=request.POST.get("next"))),
                content_type="application/json"
            )
        return HttpResponse(json.dumps(False), content_type="application/json")
    else:
        return response
else:
    form = auth_forms.AuthenticationForm()
    model = dict(login_form=form)
    return render(request, template_path, model)

登錄.html

    {% if form.non_field_errors %}
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    Login failed. Email and password case-sensitive.
                </div>
    {% endif %}  
<form id="formLogin" action="{% url 'login' %}" method="post" role="form">
            <input type="hidden" name="csrfmiddlewaretoken">
            <input type="hidden" name="next" value="/bs5" />
            <label for="id_username" class="form-label">Email address</label>
            <input type="username" class="form-control" id="id_username" name="username" value="{{ form.username.value }}">
            <label for="id_password" class="form-label">Password</label>
            <input type="password" class="form-control" id="id_password" name="password">
            <button class="btn btn-primary btn-lg" type="submit">Log In</button>
</form>

我也試過

return HttpResponseRedirect('/bs5')

但是它沒有顯示登錄錯誤。

任何幫助,將不勝感激。

如果您想重定向無法登錄到其他頁面的用戶,您可以采用的方法之一是:

from django.contrib import messages
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect

def login(request, template_path='login.html'):
    if request.user.is_authenticated:
         redirect('home')

    user = authenticate(request, username= request.POST.get('username'), password=request.POST.get('password'))
    if user is not None:
          next_url = request.POST.get('next') # get your next url
          messages.error(request, _('Invalid credentials')) # display login error on the next page
          return redirect(next_url)
    else:
         login(request, user)
         # redirect to the successful url that you wish to

有關在模板中顯示消息的更多信息,您可以查閱django 的文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM