Django错误:create_user()至少需要2个参数(给定3个)

时间:2014-03-17 18:54:23

标签: python django views

我是Django的新手,抱歉这个愚蠢的问题,但是当我尝试进行用户注册时出现了这个错误:

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'])

我的views.py如下:

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)

相应的forms.py类如下:

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.")

1 个答案:

答案 0 :(得分:2)

create_user()函数将用户名作为位置参数:

user = User.objects.create_user(
    form['username'], first_name=form['first_name'], 
    last_name=form['last_name'], password=form['password'], 
    email=form['email'])

您仍然可以将其作为关键字参数传递,但是您需要将其称为username,而不是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'])