Django密码问题 [英] Django password problems

查看:186
本文介绍了Django密码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class UserForm(forms.ModelForm):
Meta:
model = User
fields =('username','password','email',)

,但密码字段显示为常规文本字段,而不是密码输入。我如何确保它显示密码字段?



我尝试过:

  class UserForm(forms.ModelForm):
username = forms.CharField(max_length = 15,min_length = 6)
password = forms.PasswordInput()
class Meta:
model = User
fields =('username','password','email',)

但这也不起作用。



我也尝试添加一个确认密码字段,但这不会显示任何字段:

  class UserForm(forms.ModelForm):
username = forms.CharField(max_length = 15,min_length = 6)
password = forms.PasswordInput()
cpassword = forms.PasswordInput()

def clean(self):
如果self.cleaned_data ['cpassword']! = self.cleaned_data ['password']:
raise forms.ValidationError(Passwords not not match)

class Meta:
model =用户
fields =('用户名','密码','cpassword','电子邮件')


解决方案

你缺少表单域和窗体小部件之间的区别。该小部件是该字段的HTML表示。如果您使用Django 1.2,您可以使用以下语法:



编辑:更新以包含确认密码

  class UserForm(forms.ModelForm):

confirm_password = forms.CharField(widget = forms.PasswordInput())

class Meta:
model = User
fields =('username','password','email',)
widgets = {
'password':forms .PasswordInput(),
}


I'm using a modelform for User like so:

class UserForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('username','password','email',)

but the password field shows up as a regular textfield, not a password input. How do I make sure it shows a password field?

I tried this:

class UserForm(forms.ModelForm):
    username = forms.CharField(max_length = 15, min_length = 6)
    password = forms.PasswordInput() 
    class Meta:
        model = User
        fields = ('username','password','email',)

but that doesn't work either.

I'm also trying to add a confirm password field like so, but this causes no fields to be displayed:

class UserForm(forms.ModelForm):
    username = forms.CharField(max_length = 15, min_length = 6)
    password = forms.PasswordInput()
    cpassword = forms.PasswordInput()

    def clean(self):
        if self.cleaned_data['cpassword']!=self.cleaned_data['password']:
            raise forms.ValidationError("Passwords don't match")

    class Meta:
        model = User
        fields = ('username','password','cpassword','email',)

解决方案

You're missing the difference between form fields and form widgets. The widget is the HTML representation of the field. If you're on Django 1.2 you can use this syntax:

EDIT: Update to include confirm password

class UserForm(forms.ModelForm):

    confirm_password = forms.CharField(widget=forms.PasswordInput())

    class Meta:
        model = User
        fields = ('username','password','email',)
        widgets = {
            'password': forms.PasswordInput(),
        }

这篇关于Django密码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆