在 Django 中生成表单

发布于 2024-11-03 16:17:40 字数 2827 浏览 0 评论 0 原文

我想在 django 中生成动态表单。假设我有一个模型,其中包含由表单的所有字段组成的选择。如果我的用户选择文本区域选项并给出标签名称,则表单应显示带有给定标签的文本区域。它可以是文本输入、单选按钮等。

所以我的模型看起来像:

TYPE_CHOICES = (
     (u'TextField',u'TextField'),
     (u'TextArea',u'TextArea'),
)

class SocietyForm(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    form_info = models.ForeignKey(FormInfo)


class FormInfo(models.Model):
    label = models.CharField(max_length=255)
    content_type = models.CharField(max_length=100,choices=TYPE_CHOICES)
    form_info = models.ForeignKey(SocietyForm)

到目前为止一切都很好,但是当涉及到生成小部件时,我不知道这里=) 我想,如果我根据所选的选择生成一个字典,那么我可以将它分配给表单的小部件。

'''                                                                                                                                                                                                                                                          
def select_form_type(instance):                                                                                                                                                                                                                              
    dict = {}                                                                                                                                                                                                                                                
    if instance.content_type == "TextField":                                                                                                                                                                                                                 
        dict.update({'%s'instance.label:forms.TextInput()})                                                                                                                                                                                                  
    else:                                                                                                                                                                                                                                                    
        dict.update({'%s'instance.label:forms.Textarea()})                                                                                                                                                                                                   
    return dict                                                                                                                                                                                                                                              
'''

#class SocietyJoinForm(forms.ModelForm,instance):

但是,SocietyJoinForm 无法识别实例输入。那么有什么想法吗?

I want to generate dynamic forms in django. Assume that I have a model that contains choices which consists of all fields of forms. If my user selects Textarea option and gives a label name, the form shoul display a textarea with given label. It could be a textinput, radiobutton etc.

So my model looks like :

TYPE_CHOICES = (
     (u'TextField',u'TextField'),
     (u'TextArea',u'TextArea'),
)

class SocietyForm(models.Model):
    title = models.CharField(max_length=255)
    slug = models.SlugField()
    form_info = models.ForeignKey(FormInfo)


class FormInfo(models.Model):
    label = models.CharField(max_length=255)
    content_type = models.CharField(max_length=100,choices=TYPE_CHOICES)
    form_info = models.ForeignKey(SocietyForm)

So far so good, however when it comes to generate widgets, well I have no idea here = )
I thought, if I generate a dictionary according to selected choice then I can assign it to the form's widget.

'''                                                                                                                                                                                                                                                          
def select_form_type(instance):                                                                                                                                                                                                                              
    dict = {}                                                                                                                                                                                                                                                
    if instance.content_type == "TextField":                                                                                                                                                                                                                 
        dict.update({'%s'instance.label:forms.TextInput()})                                                                                                                                                                                                  
    else:                                                                                                                                                                                                                                                    
        dict.update({'%s'instance.label:forms.Textarea()})                                                                                                                                                                                                   
    return dict                                                                                                                                                                                                                                              
'''

#class SocietyJoinForm(forms.ModelForm,instance):

But, SocietyJoinForm doen't recognize instance input. So any idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦途 2024-11-10 16:17:40

我只是碰巧在查看所有较旧的问题时偶然发现了您的问题。如果您已经弄清楚了这一点,请告诉我。

IMO,最简单的方法是使用 Javascript。实际上,您无法在 Django 中更改此设置,除非您创建了自己的表单对象,在这种情况下,您很可能仍然需要使用 Javascript。你可以做这样的事情:

<script type="text/javascript">
    function display_form_obj(){
        var type_selection = document.getElementById('id_content_type');
        var label = document.getElementById('id_label');
        var dyn_obj = document.getElementById('id_dyn_obj_div');
        if( type_selection.value and label.value ){
            // Display the new object
            if( type_selection.value == 'TextField' ){
                dyn_obj.innerHTML = '<textarea ...>';  // Fill it with the info you need
            }
            else {
                dyn_obj.innerHTML = '<input type="text" ...>'; // Fill with needed info
            }
        }
        else {
            dyn_obj.innerHTML = '';
        }
    }

    function set_form_obj_events(){
        var type_selection = document.getElementById('id_content_type');
        var label = document.getElementById('id_label');
        if( label.addEventListener ){
            // Netscape, FF
            type_selection.addEventListener('change', display_form_obj, false);
            label.addEventListener('change', display_form_obj, false);
        }
        elif( label.attachEvent ){
            // IE
            type_selection.attachEvent('onchange', display_form_obj);
            label.attachEvent('onchange', display_form_obj);
        }
    }

</script>
...
<form ... onload="set_form_obj_events();">
    {{form.content_type}}
    {{form.label}}
    <div id="id_dyn_obj_div"></div>
    ...
</form>

希望有帮助。

I just happened to stumble upon your question looking at all the older ones. Let me know if you've already figured this out.

IMO, the easiest way would be to use Javascript. You wouldn't actually be able to change this in Django unless you created your own form object, in which case you'd most likely still have to use Javascript. You could do something like this:

<script type="text/javascript">
    function display_form_obj(){
        var type_selection = document.getElementById('id_content_type');
        var label = document.getElementById('id_label');
        var dyn_obj = document.getElementById('id_dyn_obj_div');
        if( type_selection.value and label.value ){
            // Display the new object
            if( type_selection.value == 'TextField' ){
                dyn_obj.innerHTML = '<textarea ...>';  // Fill it with the info you need
            }
            else {
                dyn_obj.innerHTML = '<input type="text" ...>'; // Fill with needed info
            }
        }
        else {
            dyn_obj.innerHTML = '';
        }
    }

    function set_form_obj_events(){
        var type_selection = document.getElementById('id_content_type');
        var label = document.getElementById('id_label');
        if( label.addEventListener ){
            // Netscape, FF
            type_selection.addEventListener('change', display_form_obj, false);
            label.addEventListener('change', display_form_obj, false);
        }
        elif( label.attachEvent ){
            // IE
            type_selection.attachEvent('onchange', display_form_obj);
            label.attachEvent('onchange', display_form_obj);
        }
    }

</script>
...
<form ... onload="set_form_obj_events();">
    {{form.content_type}}
    {{form.label}}
    <div id="id_dyn_obj_div"></div>
    ...
</form>

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文