Reputation: 39
I am using a model form that has 3 fields - name, country and city. My template looks like this-
<form method="post" id="personForm" data-cities-url="{% url 'ajax_load_cities' %}">
{% csrf_token %}
{{form}}
<input type="submit" value="Submit">
</form>
Following this is a bit more jQuery code that fetches the id for the "country" field using a code like this -
$("#id_country").change(function ()....
Two things I need to mention here, no where in my code I have id_country except from this jQuery code. Also I haven't set the id attribute for the country field anywhere.
So my question is , is this a thing in django or in jQuery where you can get the id for a field using id_ in front of the field name?
I read the django documentation, haven't seen anything like this. What am I missing here?
Thanks for any input.
Upvotes: 0
Views: 1859
Reputation: 21787
When one renders a Django form, it automatically renders an id
attribute on the form fields. As you noticed this id
is of the form id_<field-name>
. This is noted in the documentation in the section Working with form templates:
Note that each form field has an ID attribute set to
id_<field-name>
Hence you have an id
as id_country
because you have a form field named country
. If you were to have a field named foo
its id would be id_foo
.
Upvotes: 1