How to get form field's id in Django?

前端 未结 4 1564
庸人自扰
庸人自扰 2020-12-23 03:03

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

In the HTML I get:

I know I can g

相关标签:
4条回答
  • 2020-12-23 03:11

    This doesn't work for every form field.

    For instance {{ form.address.auto_id }} works while {{ form.address.auto_name }} will not.

    However you can use {{ form.address.html_name }} to get the equivalent answer.

    Here are the docs

    0 讨论(0)
  • 2020-12-23 03:25

    You can get the ID like this:

    {{ field.auto_id }}
    
    0 讨论(0)
  • 2020-12-23 03:26

    You can also use id_for_label:

    {{ field.id_for_label }}
    
    0 讨论(0)
  • 2020-12-23 03:30

    In Django 2 you can retrieve the ID for a specific field using {{ field.id_for_label }}

    This is documented here.

    0 讨论(0)
提交回复
热议问题