数据未在模型中保存mailvalidation

时间:2020-07-22 06:41:09

标签: django django-models django-views

我正在使用电子邮件验证创建注册页面,但是我遇到类型错误mailvalidation()收到了意外的关键字参数'user_id'

views.py

def signup(request):
if request.method=='POST':
    username=request.POST['username']
    email=request.POST['email']
    password=request.POST['password']
    cpassword=request.POST['cpassword']
    userdetails(name=username,email=email,password=password).save()
    user = userdetails.objects.get(email=email)
    token = default_token_generator.make_token(user)
    #print(user.id)
    #print(user.email)
    #print(user.name)
    #print(token)
    mailvalidation(user_id=user.id, user_email=user.email, user_name=user.name, key=token).save()
    sendmail(email, token)
    return HttpResponse('We have sent a email verification please check your email')
return render(request,'credentials/register.html')

models.py

class userdetails(models.Model):
    name=models.CharField(max_length=50)
    email=models.CharField(max_length=50)
    password=models.CharField(max_length=20)
    valid_email=models.IntegerField(default=0)
    last_login=models.DateTimeField(default=today)
    class Meta:
        db_table='user_details'

class mailvalidation(models.Model):
    user_id=models.IntegerField(default=0)
    user_email=models.EmailField(max_length=100)
    user_name=models.CharField(max_length=100)
    key=models.TextField()
    class Meta:
        db_table='mail_validation'

我遇到错误 enter image description here

1 个答案:

答案 0 :(得分:1)

在邮件验证中使用的

user_id应该ForeignKey

因此,您可以更改此

user_id = models.IntegerField(default=0)

user = models.ForeignKey(userdetails, on_delete=models.CASCADE)

此处user字段将重复为user_id,因此无需向用户添加_id