如何在Django中将多个表单附加到单个提交按钮?

时间:2012-06-08 15:41:30

标签: django-templates django-views python-2.7

这是一个非常noob-ish的问题,但我无法弄清楚。我想在我的Django模板中使用单个提交2个(或者可能最终更多)的表单。这是我在模板中所做的,但显然不对。

<html><body>
<form action="" method="post" enctype="multipart/form-data">     
        {% csrf_token %}
       <p>Please insert .raw file {{ form }} </p>
</form>
<form action="" method="post" enctype="multipart/form-data">
       <p>Please insert .xml file {{ form }} </p>
    <input type="submit" value="Confirm" />
    <input type="reset" value="Reset" class="button">
</form>

我的forms.py文件看起来像这样

from django import forms # for UploadFileForm

class DocumentForm(forms.Form):     docfile = forms.FileField(label ='选择文件',help_text ='文件大小无限制')

这是我的观点

def Upload(request):
# Handle file upload
if request.method == 'POST': # If form is submitted
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        newdoc = Document(docfile = request.FILES['docfile'])
        newdoc.save()
        # Redirect to Processing window until processing is complete
        return HttpResponseRedirect('') # Takes me right back to the upload Page
else:
return render_to_response(
    'Upload.html',
    {'form': form},
    context_instance=RequestContext(request) 
)

提前致谢!

2 个答案:

答案 0 :(得分:1)

感谢MindVirus,但我想做的事情要简单得多。这只是一个向我的Form&amp;添加更多FileFields的问题。这就是诀窍。这是解决方案: 将当前forms.py更改为:

class DocumentForm(forms.Form): 
    form1 = forms.FileField()
    form2 = forms.FileField()

答案 1 :(得分:0)