Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial value for Django form field when using custom template

There is a BooleanField in my Django form that I would like to have an initial value of True, so I defined it as:

my_checkbox = forms.BooleanField(label='My Checkbox', help_text='Some help text here', initial=True)

In my helper, I have:

helper.layout = Layout(
    Field('my_checkbox', template="custom.html")

Custom.html looks like:

<input class="checkboxinput" id="id_{{ field.name }}" name="{{ field.name }}" type="checkbox">
<span id="hint_id_{{ field.name }}" class="help-inline">{{ field.help_text }}</span>

I tried outputting the value of field.initial, but nothing shows up. I know that if I were coding this manually, I would just put <input ... checked>, and the box would be checked.

Is there some way to access the "initial" part of the field and set it in my custom template? Thanks!

like image 486
user2406467 Avatar asked Jul 08 '13 23:07

user2406467


1 Answers

You can access the initial data of the form field using

{{field.value}}
like image 126
Akshar Raaj Avatar answered Nov 03 '22 00:11

Akshar Raaj