Hello,
I'm quite perfectionist and I do have a deadline for my project !
But authentication is a little bit hard and not so well documented.
Here is my view (see comments please):
class UserRegManipulator(formfields.Manipulator):
def __init__(self):
self.fields = (
formfields.TextField(field_name="username",
validator_list=[self.userNotExists], length=30, maxlength=200,
is_required=True),
formfields.EmailField(field_name="email",
is_required=True),
formfields.PasswordField(field_name="password", length=10,
is_required=True),
)
def userNotExists(self, field_data, all_data):
try:
users.get_object(username__exact=field_data)
except:
pass
else:
raise validators.ValidationError("User already exists.")
def user_reg(request):
manipulator = UserRegManipulator()
if request.POST:
new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data)
if not errors:
new_user = users.create_user(new_data["username"],
new_data["email"], new_data["password"])
# SO FAR SO GOOD
# BUT: HOW TO AUTMAGICALLY LOG THIS NEWLY CREATED USER
WHILE REDIRECTING HIM TO THE NEXT FORM TO COMPLETE ?
return HttpResponseRedirect("/contact_reg/")
else:
errors = new_data = {}
form = formfields.FormWrapper(manipulator, new_data, errors)
return render_to_response('contact/user_registration_form',
{'form': form})
# THIS IS OK
def login(request):
manipulator = AuthenticationForm(request)
if request.POST:
errors = manipulator.get_validation_errors(request.POST)
if not errors:
request.session[users.SESSION_KEY] =
manipulator.get_user_id()
request.session.delete_test_cookie()
#HERE I WOULD LIKE TO REDIRECT TO THE SAME PAGE INSTEAD OF
THIS HARDCODED ONE:
return HttpResponseRedirect('/events/')
else:
errors = {}
#SAME AS ABOVE COMMENT
return HttpResponseRedirect('/events/')
Here is my base template:
{% load i18n %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block login %}
{% if user.is_anonymous %}
<p>Welcome. Please log in:
<form action="/login/" method="post">
<label for="id_username">{% trans 'Username:' %}</label> <input
type="text" name="username" id="id_username" />
<label for="id_password">{% trans 'Password:' %}</label> <input
type="password" name="password" id="id_password" />
<input type="submit" value="{% trans 'Log in' %}" />
</form>
or <a href="/user_reg/">register<a>.</p>
{% else %}
<p>Welcome, {{ user.username }}. <a href="/logout/">Logout</a></p>
{% endif %}
{% endblock %}
<div id="content">
{% block content %}{% endblock %}
</div>
</body>
Thanks for your help,
Olivier.