Question

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)
Was it helpful?

Solution

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 ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top