Django动态表单示例 [英] Django dynamic Form example

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

问题描述

我在Django中创建一个动态表单有一个简单的要求 - 我已经看到很多例子,但是他们似乎是不完整的,或者需要比Python和Django更广泛的知识。没有显示示例的动态部分应该如何调用:



这是表单类与Q1和Q2 - 我在表单上放置一个按钮以添加另一个
字段称为Q3 - 然后再次按下Q4:我想我得到的 init 函数是正确的:

  class testform(forms.Form):
Q1 = forms.CharField()
Q2 = forms.CharField()

def __init __(self,* args, ** kwargs)
super(testform,self).__ init __(* args,** kwargs)
self.fields ['Q%s'%i] = forms.CharField(label ='Q %i'%i)

我想在表单上放置一个按钮来添加另一个
字段称为Q3,然后再次按下Q4。




  • 如何从视图中调用_ init _function来添加这些字段?

  • 如果我使用Javascript来动态添加字段,那么如何调用 init ,以便在POST表单时我可以正确地验证Django?


解决方案

回答您的问题:无论何时初始化表单,都会调用 init ()。例如: form = testform()。这将初始化您的表单。我在这里做的是添加一些javascript来创建这些新的字段。我不知道你是否熟悉javascript,但我会假设你可以自己设计一个。



至于验证,这个可以以形式完成。首先,您将要告诉您,您使用 init ()添加了更多字段。

  class TestCharField(forms.CharField):
def __init __(self,* args,** kwargs):
super(TestCharField,self ).__ init __(* args,** kwargs)#需要*和**
def clean(self,value):
super(TestCharField,self).clean(value)


$ b class testform(forms.Form):
def __init __(self,* args,** kwargs):
super(testform ,self).__ init __(args,kwargs)

#args应该是带有单个QueryDict(GET或POST dict)
#的列表,您传递给
for k, v in args [0] .items():
如果k.startswith('Q')和k不在self.fields.keys()中:
self.fields [k] = TestCharField = v,required = True)

你可以命名你所在的领域,它不必是TestCharField。只需确保在两个地方重命名它。这将允许您制作尽可能多的字段。让我知道这是否不适合你。


I have a simple requirement for creating a dynamic form in Django - I've seen many examples but they seem to be incomplete, or require more extensive knowledge of Python and Django than I have! None show how the dynamic portion of the example should be called:

This is the form class with Q1 and Q2 - I place a button on the form to add another field called Q3 - and then Q4 if pressed again: I think I got the init function semi correct:

class testform(forms.Form):
    Q1 = forms.CharField()
    Q2 = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(testform,self).__init__(*args,**kwargs)
        self.fields['Q%s'%i] = forms.CharField(label='Q%i' % i)

I want to place a button on the form to add another field called Q3 and then Q4 if pressed again.

  • How do I call the _init_function from the view to add these fields?
  • If I use Javascript to add the field dynamically, how do I call init so that I can correctly validate with Django when I POST the form?

解决方案

To answer your question: Whenever you initialize your form, it calls init(). Ex: form = testform(). That initializes your form. What I would do here is add some javascript to create these new fields. I'm not sure how familiar you are with javascript, but I'm going to assume you can figure this one out on your own.

As for the validation, this can be done in the form. First though, you'll want to tell it that you've added more fields using init().

class TestCharField(forms.CharField):
    def __init__(self, *args, **kwargs):
        super(TestCharField, self).__init__(*args, **kwargs) # Needs * and **
    def clean(self, value):
        super(TestCharField, self).clean(value)
        # Do custom validation in here
        return value

class testform(forms.Form):
    def __init__(self, *args, **kwargs):
        super(testform, self).__init__(args, kwargs)

        # args should be a list with the single QueryDict (GET or POST dict)
        # that you passed it
        for k,v in args[0].items():
            if k.startswith('Q') and k not in self.fields.keys():
                self.fields[k] = TestCharField(initial=v, required=True)

You can name your field whatever you want, it doesn't have to be TestCharField. Just make sure to rename it in both places. This will allow you to make as many fields as you like. Let me know if this doesn't quite work for you.

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

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