Python - Django - activate() returns None
Question: Okay, so I'm trying to create a log in with Django.

from views.py:

    
    
    from django.shortcuts import *
    from forms import UserRegistrationForm
    from django.contrib.auth import authenticate, login
    from django.core.mail import send_mail
    from django.conf import settings
    import pprint
    import utils
    import models
    from django.http import HttpResponse
    import views
    from django.conf.urls import patterns, url    
    
    def login_user(request):
        state = "Please log in below..."
        username = password = ''
    
        if request.POST:
            username = request.POST.get('username')
            password = request.POST.get('password')
    
            user = authenticate(username=username, password=password)
    
            if user is not None:
                if not user.is_verified:
                    state = "Please check your e-mail to verify your account."
                elif user.is_active:
                    login(request, user)
                    state = "You're successfully logged in!"
                else:
                    state = "Your account is not active, please contact the site admin."
            else:
                state = "Your username and/or password were incorrect."
    
            return render_to_response('myapp/dashboard.html', {'state': state}, RequestContext(request))
    
        else:
            return render_to_response('myapp/login.html', RequestContext(request))
    

My login template (login.html):

    
    
    <h3>Login</h3><hr />
    
                <form action="/login/" method="post">
            {% csrf_token %}
                    {% if next %}
                    <input type="hidden" name="next" value="{{ next }}" />
                    {% endif %}
    
                    username:
                    <input type="text" name="username" value="{{ username }}" /><br />
                    password:
                    <input type="password" name="password" value="" /><br />
    
                    <input class="btn" type="submit" value="Log In" />
                </form>
    

I'm using a basic SQLite3 to get everything figured out. python manage.py
syncdb runs with no problems

from settings.py:

    
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': 'tempDB'
        }
    }
    

When I log into my admin site I see the user:

> username: nick password: the hashed version of nick

When I insert a break (using PBD) I was able to see that the app goes through
the login_user view.

    
    
    > ...myapp/myapp/views.py(52)login_user()
    -> state = "Please log in below..."
    (Pdb) n
    > ...myapp/myapp/views.py(53)login_user()
    -> username = password = ''
    (Pdb) print username
    *** NameError: name 'username' is not defined
    (Pdb) n
    > ...myapp/myapp/views.py(55)login_user()
    -> if request.POST:
    (Pdb) print username
    
    (Pdb) n
    > ...myapp/myapp/views.py(56)login_user()
    -> username = request.POST.get('username')
    (Pdb) print username
    
    (Pdb) n
    > ...myapp/myapp/views.py(57)login_user()
    -> password = request.POST.get('password')
    (Pdb) print username
    nick
    (Pdb) n
    > ...myapp/myapp/views.py(59)login_user()
    -> user = authenticate(username=username, password=password)
    (Pdb) print password
    nick
    (Pdb) n
    > ...myapp/myapp/views.py(61)login_user()
    -> if user is not None:
    (Pdb) print user
    None
    

This shows that it gets the username 'nick' and the password 'nick' (the
settings that I set the account up with) however the problem I run into is
that authenticate is still returning None which means SOMETHING is not set up
correctly / is not working.

After that login_user view the 'state' prints "Your username and/or password
were incorrect." as told to do in views.py

I would greatly appreciate it if someone would be able to help me out!


Answer: _(Replied on G+ as well)_

From your pdb session:

    
    
    (Pdb) print password
    nick
    

If that's what happens, there's a problem there, don't you agree? Also, I'd do
away with the initialization of the vars and do this instead:

    
    
    username = request.POST.get('username')
    password = request.POST.get('password')
    if username and password:
        # Your code
    

request.POST.get will work anyway (POST is always initialized as a QueryDict)
and the get() returns None if the key is not found, so you won't get any
errors - and it might be easier for you to see what's going on.﻿

