I've passed 'Tango with Django' tutorial but still don't get one thing - why we need to use hidden fields in Django template.
For example, if I have a code
class CategoryForm(forms.ModelForm):
name = forms.CharField(max_length=128, help_text="Please enter the category name.")
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
Ok, here I get, that views and likes won't be filled by user in form, so they are hidden. But, in template, inside the form we have something like:
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
Why do we need 2-nd and 3-rd rows? And do we need them?
It's difficult to say exactly why, because the reasons could be pretty numerous.
But broadly speaking, it is probably so that those two fields can be modified or accessed client-side by javascript, and then also be submitted back to the server.
If you're not doing any of that, then you probably don't need them :) But you might have to worry about not overwriting those values when you save changes to the other fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With