def register(request):
flag = True
possible = '0123456789abcdefghijklmnopqrstuvwxyz'
token = ''
length = 10
i = 0
current_datetime = datetime.datetime.now()
user = UsersModelForm()
if request.method == 'POST':
userf = UsersModelForm(request.POST)
username = userf.data['username']
password = userf.data['password']
passwordrepeat = userf.data['passwordrepeat']
email = userf.data['email']
if password != passwordrepeat:
flag = False
passVariable = {'user':user, 'flag': False}
return render_to_response('register.html', passVariable, context_instance=RequestContext(request))
elif password == passwordrepeat:
for i in range(1,10):
temp = random.choice(possible)
token = token + temp
print token
userf.email_token = token
userf.email_token_expiry = current_datetime + timedelta(1)
if userf.is_valid():
check = userf.save()
return HttpResponseRedirect('/')
userf.save()正在插入一行,但它没有保存email_token,email_token_expiry,它只是插入用户名,密码,电子邮件和默认值,因为它在models.py中。
models.py:
class Users(models.Model):
username = models.CharField(max_length=255)
slug = models.CharField(max_length=255, default='0')
password = models.CharField(max_length=300)
passwordrepeat = models.CharField('Repeat Password', max_length=300)
password_token = models.CharField(max_length=300, default='0')
email = models.CharField(max_length=255)
email_verified = models.BooleanField(default=False)
email_token = models.CharField(max_length=255)
email_token_expiry = models.DateTimeField(auto_now_add=True)
tos = models.BooleanField(default=False)
active = models.BooleanField(default=False)
last_login = models.DateTimeField(auto_now_add=True)
last_action = models.DateTimeField(auto_now_add=True)
is_admin = models.BooleanField(default=False)
role = models.CharField(max_length=255, default='0')
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.username
class UsersModelForm(forms.ModelForm):
class Meta:
model = Users
fields = ('username', 'password', 'passwordrepeat', 'email')
widgets = {
'password' : PasswordInput(),
'passwordrepeat' : PasswordInput(),
}
有人可以帮我解决这个问题吗?为什么不将值保存到其他字段?
答案 0 :(得分:1)
试试这个:
if userf.is_valid():
user = userf.save(commit=False)
user.email_token = token
user.email_token_expiry = current_datetime + timedelta(1)
user.save()
答案 1 :(得分:0)
它只插入您在fields
中指定的字段的值。将email_token,email_token_expiry添加到该列表中,这些也将保存到数据库中。
check = userf.save(commit=False)
check.email_token = token
check.email_token_expiry = current_datetime + timedelta(1)
check.save()
表单对象不会保存附加到它的附加信息(AFAIK)。您需要从save()返回对象,然后分配这些值。 commit=False
使表单不保存到数据库。然后添加所需的额外信息并保存对象。