Wednesday, March 28, 2012

IntegrityError at /register/ auth_user.username may not be NULL

I'm getting the following error with my registration section of my
program.

IntegrityError at /register/
auth_user.username may not be NULL

I am using django 1.3.1 and python 2.7.2 and I'm using sqlite3 as the
database.
I am a newbie and I'm learning Djanogo from the book "Learning website
development with Django"

The registration form is displayed ok and I'm able to fill it out and
it is when I click register that the problem occurs.

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

My views.py looks like the following and register_page is the
relevant function here:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

from django.http import Http404
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
from bookmarks.bookmarksapp.forms import RegistrationForm


def main_page(request):
return render_to_response(
'main_page.html', RequestContext(request)
)

def user_page(request, username):
try:
user = User.objects.get(username=username)
except:
raise Http404('Requested user not found')

bookmarks = user.bookmark_set.all()
variables = RequestContext(request, {
'username': username,
'bookmarks': bookmarks
})
return render_to_response('user_page.html', variables)

def logout_page(request):
logout(request)
return HttpResponseRedirect('/')

def register_page(request):
if request.method =='POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.user = User.objects.create_user(
username = form.cleaned_data['username'],
password = form.cleaned_data['password1'],
email = form.cleaned_data['email']
)
return HttpResponseRedirect('/register/success/')
else:
form = RegistrationForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response(
'registration/register.html',
variables
)

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

My forms.py is the following:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

from django import forms
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist

class RegistrationForm(forms.Form):
username = forms.CharField(label="Username", max_length=30)
email = forms.EmailField(label='Email')
password1 = forms.CharField(
label='Password',
widget=forms.PasswordInput()
)
password2 = forms.CharField(
label='Password',
widget=forms.PasswordInput()
)

def clean_password2(self):
if 'password1' in self.cleaned_data:
password1 = self.cleaned_data['password1']
password2 = self.cleaned_data['password2']
if password1 == password2:
return password2
raise forms.ValidationError('Passwords do not match.')

def clean_username(self):
username = self.cleaned_data['username']
if not re.search(r'^\w+$', username):
raise forms.ValidationError('Username can only contain
alphanumeric characters and underscores')
try:
User.objects.get(username=username)
except ObjectDoesNotExist:
return username
raise forms.ValidationError('Username already taken. ')

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

my register.html looks like the following:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

{% extends "base.html" %}
{% block title %}User Registration{% endblock %}
{% block head %}User Registration{% endblock %}
{% block content %}
<form method="post" action=".">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="register" />
</form>
{% endblock %}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I hope somebody has an idea of what the problem is all I know is it's
something to do with the database.

The exception location is:
C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py in
execute, line 234

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment