Initial Value in PasswordInput Django Form

I’m creating django form using django “User” model for editing. As usual, what I did just creating model form and create the instance of the form class and pass the “User” instance.
Here’s my form and how I used it.

class StaffForm(forms.ModelForm):

    class Meta:
        model = User
        exclude = ['groups', 'user_permissions', 'is_superuser', 'is_staff',
                   'is_active', 'date_joined', 'last_login']
        widgets = {
            'username': forms.TextInput(attrs={'class': 'form-control'}),
            'first_name': forms.TextInput(attrs={'class': 'form-control'}),
            'last_name': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
            'password': forms.PasswordInput(attrs={'class': 'form-control'})
        }

And here is once I create the instance.

form = StaffForm(request.POST or None, instance=request.user)

But once I render the form on template, the password field has no initial value otherwise the fields populated with initial data.
And here is the solution for this issue, Django PasswordInput widget has some trick to render initial value.

forms.PasswordInput(render_value=True)

Reference: https://docs.djangoproject.com/en/dev/ref/forms/widgets/#passwordinput

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s