我的模型定义为:
class MyUser(AbstractUser):
created = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=256)
phone = models.IntegerField()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username', 'name', 'phone']
MyUser._meta.get_field_by_name('email')[0]._unique=True
现在当我通过发送表单中的所有参数调用create_user时 我的view.py看起来像
def new_user(request):
email = request.POST.get('email','')
phone = request.POST.get('phone','')
name = request.POST.get('name','')
password = request.POST.get('password','')
confirm_pass = request.POST.get('confirm_password','')
if password and confirm_pass and password == confirm_pass:
try:
user = MyUser.objects.get(email=email)
except MyUser.DoesNotExist:
user = None
if user:
message = 'User with the following email id already exists'
return render(request, 'signup.html', {'message':message})
else:
user = My.objects.create_user(email, email, name, phone, password)
user.save()
login(request, user)
return render(request, 'details.html', {'username':request.user.username})
else:
message = 'Paaswords does not match each other'
return render(request, 'signup.html', {'message':message})
现在每次执行此功能时,都会显示以下错误
create_user()最多需要4个参数(给定6个)
我无法理解为什么create_user无法正常工作。
答案 0 :(得分:0)
create_user函数将参数作为* args,因此您需要调用函数:
create_user(email = email, name = name, phone = phone, password = password)
或轻松传递您的request.POST:
create_user(request.POST)
我看到你的观点有点复杂,你有没有尝试过基于类的观点? 使用基于类的视图,您可以创建方法来处理获取查询集和表单呈现。 看看https://docs.djangoproject.com/en/dev/topics/class-based-views/
最后一点建议,你可以创建一个register_form来处理数据验证: https://docs.djangoproject.com/en/dev/topics/forms/
答案 1 :(得分:0)