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

909 views
Skip to first unread message

laurence Turpin

unread,
Mar 28, 2012, 3:31:09 PM3/28/12
to Django users
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

Daniel Roseman

unread,
Mar 28, 2012, 4:38:50 PM3/28/12
to django...@googlegroups.com
On Wednesday, 28 March 2012 20:31:09 UTC+1, laurence Turpin wrote:
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:

<snip>
 
    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. ')
 
Looks like you have an indentation problem in clean_username - the lines under the first "raise forms.ValidationError" should be one level to the left, otherwise they will never be reached and the method will never return the cleaned value for username.
--
DR.

laurence Turpin

unread,
Mar 28, 2012, 5:37:42 PM3/28/12
to Django users
Thank you Danial you are absolutely right. I tried what you said and
it works fine now.
Reply all
Reply to author
Forward
0 new messages