[Django] Give an initial value to ChoiceField

environment Python 3.5.0 Django 1.9.2

forms.py


CAKE_CHOICES = (
    (1, 'Strawberry cake'),
    (2, 'Chocolate cake'),
    (3, 'Cheesecake')
)

class CakeForm(forms.Form):
	medal = ChoiceField(widget=Select, choices=MEDAL_CHOICES)
	
	class Meta:
		model = Cake

views.py


cake_form = CakeForm(initial = {
	'selected_cake': 2	#Set chocolate cake to default
})

Recommended Posts

[Django] Give an initial value to ChoiceField
[Django] Give Form a dynamic initial value from Model
Django initial setup up to Intellij Debug
[Python] [Django] How to use ChoiceField and how to add options
Steps from installing Django to displaying an html page
unable to import django
update django version 1.11.1 to 2.2
It's too easy to use an existing database with Django
[Django] How to give input values in advance with ModelForm
How to do the initial setup from Django project creation
How to check if a value exists in an enum
I'm trying to create an authentication / authorization process with Django
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]
Relationship between Bound / Unbound and initial value of Django2 Form