Django,表单字段的唯一ID

时间:2022-04-25 01:27:10

I have a simple Django form:

我有一个简单的Django表单:

class CommentForm(forms.Form):
    comment = forms.CharField(max_length=2000, required=True)
    post_id = forms.CharField(max_length=2000, widget=forms.HiddenInput, required=True)
    parent_id = forms.CharField(max_length=2000, widget=forms.HiddenInput, required=True)

Now I want to print this form several times on my page - I am doing it through a template tag, so the new form is created each time. The problem is, that I get the same ID's for all fields.

现在我想在我的页面上多次打印这个表单 - 我是通过模板标签来完成的,所以每次都会创建新表单。问题是,我获得了所有字段的相同ID。

I know about the prefix, but I do not want to change field names, because there is one handler for all forms, only to set unique IDs.

我知道前缀,但我不想更改字段名称,因为所有表单都有一个处理程序,只能设置唯一ID。

So my question:

所以我的问题:

  • Is there a way to make Django set unique IDs if I want to output a form several times, without changing the names of fields?
  • 有没有办法让Django设置唯一的ID,如果我想多次输出一个表单,而不改变字段的名称?
  • If not, is there a way to make Django not to output IDs at all?
  • 如果没有,有没有办法让Django根本不输出ID?

1 个解决方案

#1


11  

You can control how the automatic IDs are generated with the auto_id parameter when you create a new instance of that form

您可以在创建该表单的新实例时使用auto_id参数控制自动ID的生成方式

Have a look here (search for auto_id):

看看这里(搜索auto_id):

http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags

http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags

#1


11  

You can control how the automatic IDs are generated with the auto_id parameter when you create a new instance of that form

您可以在创建该表单的新实例时使用auto_id参数控制自动ID的生成方式

Have a look here (search for auto_id):

看看这里(搜索auto_id):

http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags

http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags