I'm trying to follow the code from http://docs.djangoproject.com/en/1.1/topics/auth/ regarding user logins.
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
HttpResponseRedirect('/%s/'%username)
else:
#Logic will go here later
pass
else:
# Return an 'invalid login' error message.
pass
My problem is that when I attempt to use this new view, I get the following error message: ('expected an indented block', ('C:\emodel_tracking\..\emodel_tracking\tracker\views.py', 50, 4, ' else:\n'))
I can't see how the formatting on the else statement could be wrong; I'm using the code straight from the django documentation. Any ideas what's wrong with the "else" statement?
edit: OK, using pass fixed that error - now I'm getting guff about: MultiValueDictKeyError at /login/ --- Key 'username' not found in QueryDict: {}. Thoughts? I'm not quite sure how to proceed - my guess is that I need to somehow tie this to an html form similar to this one:
{% extends "base.html" %}
{% block content %}
{% if form.errors %}
<p class="error">Sorry, that's not a valid username or password</p>
{% endif %}
<form action="" method="post">
<label for="username">User name:</label>
<input type="text" name="username" value="" id="username">
<label for="password">Password:</label>
<input type="password" name="password" value="" id="password">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{ next|escape }}" />
</form>
{% endblock %}
If this is the case, how do I render out the form (which if I'm not mistaken, will require a 'return' statement) prior to me requesting the information that form requires?