python Django:使用 Django 表单创建 HTML 输入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2420094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:37:08  来源:igfitidea点击:

Django: create HTML input array using a django form

pythondjangodjango-forms

提问by Brant

I am trying to automate the creation of something like this:

我正在尝试自动创建这样的东西:

<input type='text' name='asdf[]' />
<input type='text' name='asdf[]' />
<input type='text' name='asdf[]' />

By cycling through a range in the form. I've been trying things like this, along with several other variations:

通过在窗体中循环遍历一个范围。我一直在尝试这样的事情,以及其他一些变化:

# in a model class
for i in range(1, prim+1):
    self.fields['asdf'] = forms.CharField(label=i)

# in the template
<form action='#' method='post'>
    {{form.as_p}}
</form>

But I haven't had any luck though.

但我没有任何运气。

How can I go about automating an array of inputs?

我怎样才能自动化一系列输入?

** edit ** To clarify, eventually I need to be able to access the fields in the template like this:

** 编辑 ** 澄清一下,最终我需要能够像这样访问模板中的字段:

{% for input in form.fields.asdf %}
{{input}}
{% endfor %}

Which would then hopefully get me the original input list shown above...

然后希望让我得到上面显示的原始输入列表......

采纳答案by Brant

It looks like I can do what I need to do by breaking the form into multiple formsets...

看起来我可以通过将表单分成多个表单集来做我需要做的事情......

http://docs.djangoproject.com/en/dev/topics/forms/formsets/#topics-forms-formsets

http://docs.djangoproject.com/en/dev/topics/forms/formsets/#topics-forms-formsets

Then, I should be able to access each formset individually from the template, wrapping all of them into one

然后,我应该能够从模板单独访问每个表单集,将它们全部包装成一个

回答by Dan Breen

Jacob Kaplan-Moss (co-author of Django) recently posted a great article for handling dynamic forms, which should solve your problem in a preferred way: http://jacobian.org/writing/dynamic-form-generation/

Jacob Kaplan-Moss(Django 的合著者)最近发表了一篇关于处理动态表单的精彩文章,它应该可以以首选方式解决您的问题:http: //jacobian.org/writing/dynamic-form-generation/

He's using the same method that Felix suggests, but it's worth reading the whole article to get a better grasp on the concept.

他使用的方法与 Felix 建议的方法相同,但值得阅读整篇文章以更好地掌握这个概念。

Using the asdf[]technique is sloppy, because then you have to deal with ordering. It's also not the standard practice.

使用该asdf[]技术是草率的,因为那时您必须处理排序。这也不是标准做法。

Edit:

编辑:

To handle the situation where you need to detect when you hit these dynamic fields:

要处理需要检测何时点击这些动态字段的情况:

{% for input in form.fields %}
    {% ifequal input.label 'asdf' %}
        {{ forloop.counter }}: {{input}}<br />
    {% endifequal %}
{% endfor %}

回答by Felix Kling

It should be more like e.g.:

它应该更像是例如:

# in a model class
for i in range(1, prim+1):
    self.fields['asdf_%s' % i] = forms.CharField(label='Label %i' % i)

But it very depends on what you want to achieve.

但这在很大程度上取决于您想要实现的目标。