为修改后的用户Auth django创建ModelForm

时间:2011-08-31 02:33:30

标签: django forms model

我有以下代码:

class UserForm(ModelForm):
    email = forms.EmailField(widget = forms.TextInput(attrs ={ 'id':'email'}), required=True)
    first_name = forms.CharField(widget = forms.TextInput(attrs = {'id':'fname'}), required=True)
    last_name = forms.CharField(widget = forms.TextInput(attrs = {'id':'lname'}), required=True)
    linked_id = forms.CharField(widget = forms.HiddenInput(attrs = {'id':'linkedid'}))
    password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput)
    password2 = forms.CharField(label=_('Re-Enter your password'), widget = forms.PasswordInput)

    class Meta:

        model = UserProfile

        fields = ('email', 'first_name', ... (other fields here))

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1", "")
        password2 = self.cleaned_data['password2']
        if password1 != password2:
            raise forms.ValidationError(_("The passwords you entered did not match!"))
        return password2

基本上,我正在尝试创建修改后的用户注册表单。表格实际上显示出来,这很好。但是如果用户试图实际注册。并非所有信息都存储在我的数据库中。只保存名字和电子邮件......

EDIT ::::::

抱歉,伙计们,我大部分时间都在工作,字段部分设置不正确。但是我遇到了另一个问题。当我在我的网站上查看测试用户时,它不会加密密码,它只是显示密码。如何让它加密,如何应该加密?

1 个答案:

答案 0 :(得分:1)

我建议你重新阅读文档here。如果您在理解其工作原理时遇到问题,则必须查看源代码。

以下是使用标签创建密码字段的方法。

password1 = forms.CharField(widget=forms.PasswordInput(attrs={'class':'input_text'}), label='Password')   

古德勒克。