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)
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()
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):
passwordrepeat = forms.PasswordInput()
class Meta:
model = Users
fields = ('username', 'password', 'email')
widgets = {
'password' : forms.PasswordInput(),
}
def clean(self):
cleaned_data = self.cleaned_data
password = cleaned_data.get("password")
passwordrepeat = cleaned_data.get("passwordrepeat")
if password != passwordrepeat:
raise forms.ValidationError("Passwords must match.")
return cleaned_data
在模板中我得到了
username
password
email
我无法在模板中获取passwordrepeat
输入字段。如何设置'passwordrepeat'字段的标签。标签为Repeat Password
我可以省略def clean
中的UsersModelForm
吗?我想在views.py中比较password
和repeatpassword
,而不是在models.py。
views.py:
def register(request):
flag = True
possible = '0123456789abcdefghijklmnopqrstuvwxyz'
token = ''
tempToken = ''
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']
tempSalt = bcrypt.gensalt()
password = bcrypt.hashpw(password,tempSalt)
passwordrepeat = bcrypt.hashpw(passwordrepeat,tempSalt)
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
if userf.is_valid():
check = userf.save(commit=False)
check.email_token = token
check.email_token_expiry = current_datetime + timedelta(1)
check.password = password
check.passwordrepeat = passwordrepeat
check.save()
subject, from_email, to = 'hello', 'xxx@gmail.com', 'xxx@yahoo.com'
text_content = 'This is an important message.'
html_content = '<a href="http://127.0.0.1:8000/confirm/' + token + '">Click this link to confirm email</a>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
return HttpResponseRedirect('/')
else:
return render_to_response('register.html', {"user": user, 'flag': True}, context_instance=RequestContext(request))
答案 0 :(得分:5)
正确答案:使用django-registration。和django内置auth系统。不要重新发明轮子。
回答你的问题:你没有看到passwordrepeat
,因为forms.PasswordInput
不是字段,而是小部件。如果没有字段,则无法呈现窗口小部件。您应该在CharField
使用PasswordInput
。如果你想设置标签,设置它,label
参数看起来很适合这个任务。
passwordrepeat = forms.CharField(widget=forms.PasswordInput, label=u'Repeat Password')
表格
class UsersModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(UsersModelForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = ['username', 'password', 'passwordrepeat', 'email']
# ... other fields and methods ...