environment Python 3.5.0 Django 1.9.2
forms.py
class CakeForm(forms.Form):
cake_num = IntegerField(label='Number of cakes', min_value=0)
class Meta:
model = Cake
views.py
def edit(request, store_id):
#If you can get cake, set the initial value
try:
cake = Cake.objects.get(store=store_id)
cake_form = CakeForm(initial = {
'cake_num': cake.cakenum #initial value
})
#If cake cannot be obtained, the initial value is not set.
except:
cake_form = CakeForm()
References Put dynamic initial values in Django's Form--i2bs Diary
Recommended Posts