gpt4 book ai didi

python - Django错误: create_user() takes at least 2 arguments (3 given)

转载 作者:行者123 更新时间:2023-12-01 05:20:41 26 4
gpt4 key购买 nike

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

最佳答案

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

关于python - Django错误: create_user() takes at least 2 arguments (3 given),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com