隐藏密码字段Django的手动密码

时间:2018-12-27 06:22:13

标签: django django-models django-forms django-templates django-views

我正在使用django的用户内置模型。我想为每个人(如“ test @ 123”)创建用户密码。在模板中不显示密码字段

register.py

@login_required
def register(request):
    if request.method == 'POST':
        ur_form = UserRegisterForm(request.POST)
        pr_form = UserProfileForm(request.POST, request.FILES)
        if ur_form.is_valid() and pr_form.is_valid():

            new_user = ur_form.save(commit=False)            
            new_user.username = new_user.email
            password = 'test@123'
            new_user.password = password
            new_user.save()

            profile =  pr_form.save(commit=False)
            if profile.user_id is None:
                profile.user_id = new_user.id
                profile.user_role_id = 3
            profile.save()

            username = ur_form.cleaned_data.get('username')
            messages.success(request, 'Your Account has been created for %s!' % username)
            return redirect('users')
    else:
        ur_form = UserRegisterForm()
        pr_form = UserProfileForm()
    return render(request, 'users/register.html', {'ur_form': ur_form, 'pr_form': pr_form})

Forms.py

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()
    first_name = forms.CharField()
    last_name = forms.CharField()
    class Meta:
        model = User
        fields = ['first_name','last_name', 'email']

    def __init__(self, *args, **kwargs):
       super(UserRegisterForm, self).__init__(*args, **kwargs)
       del self.fields['password1']
       del self.fields['password2']

3 个答案:

答案 0 :(得分:0)

将此行new_user.password = password更改为new_user.password1 = user.set_password(password)

还将行new_user = ur_form.save(commit=False固定为new_user = ur_form.save(commit=False)

删除此表格form.py

def __init__(self, *args, **kwargs):
       super(UserRegisterForm, self).__init__(*args, **kwargs)
       del self.fields['password1']
       del self.fields['password2']

最终编辑:UserRegisterForm

new_user = f.save(commit=True)
new_user.password = new_user.set_password('sdfasd')

forms.py

class UserRegisterForm(ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email', )

答案 1 :(得分:0)

您可以使用set_password方法。例如;

new_user = ur_form.save(commit=False)            
new_user.username = new_user.email
password = 'test@123'
new_user.set_password(password) # <- here
new_user.save()

来自documentation

  

将用户密码设置为给定的原始字符串,并注意密码哈希。不保存用户对象。

答案 2 :(得分:0)

我做到了

class RegisterForm(UserCreationForm):
   credit = forms.DecimalField(decimal_places=1,max_digits=10)

   class Meta:
       model = User
       fields = ["email"]

   def __init__(self, *args, **kwargs):
       super(UserCreationForm, self).__init__(*args, **kwargs)
       self.fields['password1'].required = False
       self.fields['password2'].required = False
       # If one field gets autocompleted but not the other, our 'neither
       # password or both password' validation will be triggered.
       self.fields['password1'].widget.attrs['autocomplete'] = 'off'
       self.fields['password2'].widget.attrs['autocomplete'] = 'off'
       self.fields['password1'].widget = forms.HiddenInput()
       self.fields['password2'].widget = forms.HiddenInput()