Skip to content

Adding a radio button using django-unicorn

I was building a form today in django-unicorn and wanted to implement a radio button.

This is how you do it:

<input type="radio" id='freq_once_off' unicorn:model="new_event_frequency" 
     name="frequency" value="once_off">
<label for="freq_once_off">Once Off</label>

<input type="radio" id='freq_recurring' unicorn:model="new_event_frequency" 
    name="frequency" value="recurring">
<label for="freq_recurring">Recurring</label>

This will render to:

The main trick for rendering radio buttons is to write unicorn:model="new_event_frequency" in each radio html tag.

Then you would style from there.

In the model part of your component (the bit with UnicornView) add:

new_event_frequency='once_off' to give it a default value.

Then you can access the variable in your UnicornView via self.new_event_frequency

Leave a Reply