Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form fields' id in Django

Is there any way to get the id of a field in a template?

In the HTML I get: <input name="field_name" id="id_field_name"...

I know I can get the name with {{ field.html_name }}, but is there anything similar for getting the id?
Or can I only get it like this: id_{{ field.html_name }}?

like image 574
user182945 Avatar asked Sep 21 '10 18:09

user182945


People also ask

How do I get form fields in Django?

Basically to extract data from a form field of a form, all you have to do is use the form. is_valid() function along with the form. cleaned_data. get() function, passing in the name of the form field into this function as a parameter.

What is form Cleaned_data in Django?

form. cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).

What are form fields?

The form body contains Field elements that define how each element of the Web page appears and behaves. Each Field can contain other fields, each with its own display component. Form fields comprise several parts, which are encapsulated by the <Field> tag set: Value Expressions.

What is forms PY in Django?

forms.py is where the django documentation recommends you place all your forms code; to keep your code easily maintainable. Also, since its a convention mentioned in the documentation, it helps when you are collaborating with others because that is where others will expect to look for your code dealing with forms.


2 Answers

You can get the ID like this:

{{ field.auto_id }} 
like image 116
Will Hardy Avatar answered Sep 28 '22 02:09

Will Hardy


You can also use id_for_label:

{{ field.id_for_label }} 
like image 37
Eren Süleymanoğlu Avatar answered Sep 28 '22 01:09

Eren Süleymanoğlu