This code demonstrates two simple techniques:
so-called "dynamic" forms, in which the form is created at run time by the model
using a widget (forms.widgets.HiddenInput) for a field of the form. I feel like this could be highlighted more in the documentation. You need to do something similar to get a textarea (forms.CharField(widget=forms.widgets.Textarea()) and it took me too long to figure this out.
There are, no doubt, good reasons not to do what I'm doing here the way I'm doing it. Peanut gallery?
1 2 3 4 5 6 7 8 9 10 11
from django import forms hiddenValues = forms.Form() value0 = 17 hiddenValues.fields['value0'] = forms.CharField(initial=value0, widget=forms.widgets.HiddenInput()) # pass the form to your template # render it as you like, e.g. with form.as_p
More like this
- codigo alto nivel by MrRocklion 4 weeks ago
- Load template from specific app by Krzysiek555 1 month, 2 weeks ago
- PostgreSQL JSON subqueries by dolamroth 1 month, 3 weeks ago
- "Magic Link" Management Command by webology 6 months, 3 weeks ago
- Closest ORM models to a latitude/longitude point by simonw 6 months, 3 weeks ago