Reputation: 23
I'm a newbie with Django, sorry for the silly question, but I get this error when I try to do the user registration :
create_user() takes at least 2 arguments (3 given) in the line
user = User.objects.create_user(user=form['username'], first_name=form['first_name'], last_name=form['last_name'], password=form['password'], email=form['email'])
My views.py is the following:
def register(request):
context = RequestContext(request)
# check if the request contains POST data
# this happens when a user submits a form
if request.POST:
print 'is a post'
#create form object
form = RegistrationForm(request.POST)
if form.is_valid():
print 'form is valid'
user = User.objects.create_user(user=form['username'], first_name=form['first_name'], last_name=form['last_name'], password=form['password'], email=form['email'])
user.save()
#hash password with the set_password method
user.set_password(user.password)
user.save()
print 'saved user'
upc = UserPreferredCities.objects.create(user=User.objects.get(username=user.username),
city=City.objects.get(city=user.city), )
upc.save()
print 'form is saved'
if authenticate(username=user.username, password=user.password) is None:
print 'could authenticate'
print 'is authenticated'
# return response and redirect user to Bookings page
return HttpResponseRedirect('/bookings/', context)
# Show the city selection page if not authenticated.
cities = []
all_cities = City.objects.all().order_by('city')
for city in all_cities:
cities.append(city)
context_dict = get_context_dictionary(request)
context_dict['cities'] = cities
return render_to_response('register.html', context_dict, context)
The corresponding forms.py class is the following:
class RegistrationForm(forms.Form):
username = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=64)))
email = forms.EmailField(widget=forms.TextInput(attrs=dict(required=True, max_length=64)))
first_name = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=64)))
last_name = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=64)))
password = forms.CharField(widget=forms.PasswordInput(attrs=dict(required=True, max_length=64, render_value=False)))
city = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=64)))
class Meta:
model = User
fields = ('username', 'email', 'first_name', 'last_name', 'password', 'city', )
def clean_username(self):
try:
User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError("The username already exists. Please try another one.")
Upvotes: 2
Views: 4108
Reputation: 1123440
The create_user()
function takes the username as a positional argument:
user = User.objects.create_user(
form['username'], first_name=form['first_name'],
last_name=form['last_name'], password=form['password'],
email=form['email'])
You can still pass it in as a keyword argument, but then you need to call it username
, not user
:
user = User.objects.create_user(
username=form['username'], first_name=form['first_name'],
last_name=form['last_name'], password=form['password'],
email=form['email'])
Upvotes: 3