1👍
✅
First of all, note that the readonly
attribute is not safe to prevent changes. Any nitwit can change the html code nowadays with inspect element etc. built into any browser. For security reasons it’s better to exclude the field from the form and present the data in another way, or to have a server-side check to prevent people from changing data.
That’s also a good way to solve your question. As an instance of a ModelForm
, your form has an instance
attribute which holds the actual data related to the object, given that this data exists. Instead of going through an unused form field, use the data form the instance as-is:
<input type="text" readonly="readonly">{% firstof form.instance.activity.name "<default>" %}</input>
👤knbk
Source:stackexchange.com