本文介绍了Django错误:create_user()至少需要2个参数(3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Django的新手,对于这个愚蠢的问题抱歉,但当我尝试进行用户注册时,我收到这个错误: create_user()在行
user = User.objects.create_user(user = form ['username'],first_name = form ['first_name']中至少包含2个参数(3) last_name = form ['last_name'],password = form ['password'],email = form ['email'])
我的views.py如下:
def注册(请求):
context = RequestContext(request)
#检查请求是否包含POST数据
#当用户提交表单
(如果request.POST:
) print'是一个帖子'
#create表单对象
form = RegistrationForm(request.POST)
如果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密码与set_password方法
user.set_password(user.password)
user.save()
打印'保存的用户'
upc = UserPreferredCities.objects.create(user = User。 object.get(username = user.username),
city = City.objects.get(city = user.city),)
upc.save()
print'
如果验证(username = user.username,password = user.password)为无:
print'可以验证'
print'被认证'
#return响应并将用户重定向到预订页面
返回HttpResponseRedirect('/ bookings /',上下文)
#显示城市选择页面,如果不通过验证。
cities = []
all_cities = City.objects.all()。order_by('city')
全部城市中的城市:
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,rend er_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'])
除了User.DoesNotExist:
return self.cleaned_data ['username']
raise forms.ValidationError(该用户名已存在。请尝试另一个。)
解决方案
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.objects.create_user(
username = form ['username'],first_name = form ['first_name'],
last_name = form [' last_name'],password = form ['password'],
email = form ['email'])
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.")
解决方案
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'])
这篇关于Django错误:create_user()至少需要2个参数(3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!