[Django]-Setting value of a hidden field in Django form

7👍

So for your answer you can do as you said in comment, but directly from the field definition :

email = forms.EmailField(
    widget=forms.HiddenInput(),
    required = False,
    initial="dummy@freestuff.com"
)

Or just declare a form without an email field (so in your example : username, password1 and password2) and treat the username / email part in the form’s save method :

def save(self, commit=True):
    user = super().save(commit=False) # here the object is not commited in db
    user.email = self.cleaned_data['username']
    user.save()
    return user

There you don’t have to hide a field with a dummy value, which i think is "cleaner".

Leave a comment