[Django]-Django crispy forms – Set hidden field value

4πŸ‘

βœ…

You can set Form field initial values like this:

class ConsultForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.initial['TEMPLATE'] = 'my_initial_value'

You can also change the value of the field at other places in your code like:

form = ConsultForm(instance=instance)
form.initial['TEMPLATE'] = 'new_value'

With formhelper (with crispy Universal Layout Objects like Field) you set attributes as you already did, like:

Field('TEMPLATE', id="template", value="something" template="my-template.html")

If that’s what you were asking for.

Or if the above does not work easy then there is a layout object called Hidden in crispy. You can create hidden input fields with that:

Hidden('name', 'value')

You use it as Hidden('TEMPLATE', 'mysomethingvalue')

Like:

Button('name', 'value')

To make it fully clear:

helper.layout = Layout(
        Hidden('TEMPLATE', 'myvalue'),
        Hidden('DATE', 'anydate'))
πŸ‘€Zollie

Leave a comment