WEIRD Django AuthenticationForm Behaviour
<p>I have, all day, tried to figure this out but I can't really see where the problem is coming from.</p>

<p>I have a <strong>Django</strong> <code>AuthenticationForm</code> that seems to be submitting data somehow but not getting validated.</p>

<p><strong>forms.py</strong>:</p>

<pre><code>class LoginForm(AuthenticationForm):
    username = forms.CharField(widget=forms.TextInput(attrs={'name': 'username'}))
    password = forms.CharField(widget=forms.PasswordInput(attrs={'name': 'password'}))
</code></pre>

<p><strong>views.py</strong>:</p>

<pre><code>def index(request):
    template = 'myapp/login.html'
    if request.method == "POST":
        print request.POST #prints QueryDict with its data
        reg = LoginForm(request.POST or None)
        if reg.is_valid():
            return HttpResponse('Success: Form is valid!')              
        else:
            return HttpResponse('Error: Form not valid')

    loginform = LoginForm()
    context = {'loginform':loginform}
    return render(request, template, context)
</code></pre>

<p><strong>HTML</strong>:</p>

<pre><code>    &lt;form method="post" action="."&gt;
        {% csrf_token %}
        &lt;h2&gt;Sign in&lt;/h2&gt;
        &lt;p class="text-danger"&gt;{{loginform.errors}}&lt;/p&gt;
        {{ loginform.as_p }}    
        &lt;button name = "signin" type="submit" value="0"&gt;Sign in&lt;/button&gt;
    &lt;/form&gt;
</code></pre>

<p>The <code>print request.POST</code> in my views.py prints <em>QueryDict: {u'username': [u'yax'], u'csrfmiddlewaretoken': [u'r8y1PaVNjxNWypdzP
MaFe1ZL7IkE1O7Hw0yRPQTSipW36z1g7X3vPS5qMMX56byj'], u'password': [u'sdfdsfsddfs']
, u'signin': [u'0']}</em> but the <code>reg.is_valid()</code> keeps returning <code>false</code>.</p>

<p><strong>EDIT</strong>:</p>

<p>I have also tried printing out <code>reg.errors</code> but it doesn't print out anything.</p>

<p>Try making the following change:</p>

<pre><code>reg = LoginForm(data=request.POST)
</code></pre>

<p><code>AuthenticationForm</code> is slightly different in that it needs the <code>data</code> argument.</p>

<p>Also note that you should test it with actually valid username and password combinations (that correspond to an existing user), since <code>AuthenticationForm</code> checks for that as well.</p>

python django python-2.7 django-forms