简体   繁体   English

通过使用javascript单击按钮动态添加Django表单

[英]add django forms dynamically by clicking button using javascript

ok. 好。 so I am trying to create a form so that, when I a button "+" a new form appears beneath the existing one. 因此,我试图创建一种表单,以便当我单击“ +”按钮时,新表单出现在现有表单的下方。 For example I have form to fill name 例如我有填写姓名的表格

VoteType
name: [        ]
{+}  -- (button)

when clicked "+" 当点击“ +”

the form will look like this 表格看起来像这样

VoteType
name: [        ]

Vote1 name [        ]
      image [        ]
      date [        ]

{+} -- (button)

and the button "+" so that I can add Vote2 ... as many as I want. 和“ +”按钮,以便我可以根据需要添加Vote2...。

I tried to look for the ways to do it and couldn't find any solution. 我试图寻找实现方法,但是找不到任何解决方案。

this is my current createview in views.py: 这是我当前在views.py中的createview:

def create(request):
    voteTypeForm = VoteTypeForm(request.POST or None)
    voteForm = VoteType(request.POST or None)
    clicked = 0
    instance = voteTypeForm.save(commit=False)
    instance.pub_date = timezone.now()
    instance.save()
    #print instance.pub_date
    context = RequestContext(request,{
            'voteTypeForm': voteTypeForm,
            'clicked': clicked,
            'voteForm': voteForm,
    })
    return render(request, 'Vote/create.html', context)

I couldn't find how to change variable clicked that is in my views from this template. 我找不到从此模板更改视图中点击的变量的方法。 how should I use button so that I change clicked value? 如何使用按钮来更改点击值? or if you have better solutions what should I do for this problem? 或者,如果您有更好的解决方案,该怎么办? I was told that this is more javascript problem rather than django code. 有人告诉我,这是更多的JavaScript问题,而不是Django代码。 So I edited the code create.html code adding javascript: ( got the code from here ) 所以我编辑了代码create.html,添加了javascript :( 从此处获得了代码

<form method = 'POST' action = ''>{%csrf_token %}
    {{ voteTypeForm }}
    <input type = 'submit' value="create"/>
</form>
<fieldset id="fieldset">
    <legend id="legend">Voting Candidates</legend>
    <div id="placeholder">
        <form method = 'POST' action = ''>{%csrf_token %}
            {{ voteForm }}
        </form>
    </div> <!-- placeholder -->
    <p><button type="button" name="Submit" onclick="Add();">+</button></p>
</fieldset>
<script>
    var _counter = 0;
    function Add() {
        _counter++;
        var oClone = document.getElementById("template").cloneNode(true);
        oClone.id += (_counter + "");
        document.getElementById("placeholder").appendChild(oClone);
    }
</script>

but this code doesn't work either. 但是此代码也不起作用。 I am pretty bad at javascript, so I don't understand whats wrong in the code. 我对javascript很不好,所以我不明白代码中的错误。 Can you tell me how to fix this code? 您能告诉我如何解决此代码吗?

I had a similar need and ended up modifying this underscore.js tutorial . 我有类似的需求,最终修改了此underscore.js教程

Another user explained how they used the same tutorial but without using underscore here . 另一个用户解释了他们如何使用同一教程,但此处未使用下划线。

They're both pretty extensive, so I'm not going to recreate anything here. 它们都相当广泛,因此在这里我不会再创建任何东西。

In all cases though, you're going to need to use an inlineformset_factory ( docs ) so you can save multiple instances of the form. 不过,在所有情况下,都需要使用inlineformset_factorydocs ),以便可以保存表单的多个实例。

There were several problems here. 这里有几个问题。

the main one is that I had written VoteType in views instead of VoteForm. 最主要的是我在视图中编写了VoteType而不是VoteForm。 That messed everything up. 那把一切搞砸了。 After fixing that I had problem with displaying the forms with correct number and didn't want to have first for displayed in HTML, but wanted it to be stored somewhere in javascript. 修复后,我在显示带有正确数字的表单时遇到了问题,并且不想首先显示在HTML中,而是希望将其存储在javascript中。 For those problems I asked help on my 2nd question . 对于这些问题,我向第二个问题求助。 The full code solution to these problems will be provided there. 将在此处提供针对这些问题的完整代码解决方案。

For the answer of this question, it would be just Typo of VoteType and VoteForm. 对于这个问题的答案,可能只是VoteType和VoteForm的错别字。

I fixed the code add some parts and the solution is like this. 我修复了代码,添加了一些部分,解决方案是这样的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM