Django Login Form- Logs into the dashboard 2nd time

与世无争的帅哥 提交于 2019-12-11 19:04:03

问题


I've a login form. Whenever I sign into it for the first time, it asks me the username and password again, without showing me inavlid username and password. But when I login again with the same username and password it logs in perfectly. How is it possible?

Here is my code:-

{% extends "base1.html" %}
    {% block header %} 
    {% endblock %}

    {% block content %}
        <div class="wrap">
            <div class="content-wrap">
                <a href="#" class="logo"></a>
                <form action ="." method = POST class="form-signin" class="well form-inline">
                    <h3 class="form-signin-heading"></h3>
                    {% csrf_token %}    
                    {{ form.as_p }}
                    <input type = "submit" value = "login" class="btn btn-primary">
                </form>
            </div>
        </div><!--end of wrap-->

    {%endblock%}        

My urls.py file

from django.contrib.auth.views import login, logout
from django.conf.urls import *

urlpatterns = patterns('',
    url(r'login', login,kwargs = {'template_name' : 'auth/login.html'}, name = 'xyz_login'),
    url(r'logout', logout,kwargs = {'template_name' : 'auth/logout.html'}, name = 'xyz_logout'),
)

settings.py file

LOGIN_REDIRECT_URL  = '/dashboard/'
LOGIN_URL           = '/login/'
AUTHENTICATION_BACKENDS = ('modules.data.backend.MyModelBackend',)

backend.py

from django.contrib.auth.backends import ModelBackend

class MyModelBackend(ModelBackend):
    def authenticate(self,username=None, password=None):
        return super(MyModelBackend,self).authenticate(username=username,password=password)

回答1:


As you can see in these logs:

1st :

[28/Apr/2014 06:49:22] "POST /dashboard/ HTTP/1.1" 302 0 
[28/Apr/2014 06:49:22] "GET /login/?next=/dashboard/ HTTP/1.1" 200 1536

2nd :

[28/Apr/2014 06:50:27] "POST /login/dashboard/ HTTP/1.1" 302 0 
[28/Apr/2014 06:50:27] "GET /dashboard/ HTTP/1.1" 200 11292

The 1st time you are doing a POST request to /dashboard url, which must be login_required. Hence, you get a 302 redirect to /login/?next=/dashboard/

The 2nd time you POST correctly on /login. Hence you get logged in and redirected to /dashboard

Now you will have to debug and see why the first POST request is being made to /dashboard rather than /login ?



来源:https://stackoverflow.com/questions/23306953/django-login-form-logs-into-the-dashboard-2nd-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!