Search code examples
formsdjango-viewspasswordsupdateview

Django - Modelform with override password field : initial value not visible (and not saved) in Based UpdateView


I use a form for handling FTP/SFTP (files) accesses. They can be sometimes updated. My password field is a Charfield (not required) in my model but I wan't to hide it on updates. I use Bcrypt when saving it.

For this I followed the example here for adding the PasswordInput() widget to my field. I use Crispy so my form in my template is just displayed with {% crispy form %}.

class FlowStockAutoForm(forms.ModelForm):
    class Meta:
        model = FlowStockAuto
        exclude = ['fl_st_auto_report_cross_import', ]
    fl_st_auto_pwd = forms.CharField(widget=forms.PasswordInput, 
        label="Password",
        required=False, help_text="BlaBla")

    def __init__(self, *args, **kwargs):
        super(FlowStockAutoForm, self).__init__(*args, **kwargs)
        pwd = self.instance.fl_st_auto_pwd
        self.fields['fl_st_auto_pwd'].initial = pwd

In Django Doc, it's recommended to set an initial value with init (as done above) but when the password widget is active, on my update template, the field remains empty. So if I just wanna update another field, when I save, password is saved blank. It's ok when the Password Widget is off.

I would like it to be prefilled but "blurred" and check when saving if its value has changed.

What do I miss ?


Solution

  • Visibly almost duplicated question

    had to change from

    fl_st_auto_pwd = forms.CharField(widget=forms.PasswordInput, 
            label="Password",
            required=False, help_text="BlaBla")
    

    to

        fl_st_auto_pwd = forms.CharField(
                widget=forms.PasswordInput(render_value=True))
    

    by adding render_value=True

    I also had to remove

    label="Password", required=False, help_text="BlaBla"
    

    and replace it with

    self.fields['fl_st_auto_pwd'].label = 'Password'
    self.fields['fl_st_auto_pwd'].help_text = 'BlaBla'