Django,表单字段的唯一ID [英] Django, unique IDs for form fields

查看:136
本文介绍了Django,表单字段的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Django表单:

I have a simple Django form:

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)

现在我要在我的页面上打印此表单多次 - 我通过一个模板标签来做,所以每次都创建新的表单。问题是,我得到所有字段的相同的ID。

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.

所以我的问题:


  • 有没有办法使Django设置唯一的ID,如果我想输出一个表单多次,而不更改字段的名称?

  • 如果没有,有没有办法使Django不输出ID?

推荐答案

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

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 ):

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

这篇关于Django,表单字段的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆