【问题标题】:How Can I attach multiple forms to a single submit button in Django?如何将多个表单附加到 Django 中的单个提交按钮?
【发布时间】:2012-06-12 16:43:39
【问题描述】:

这是一个非常菜鸟的问题,但我无法弄清楚。我想在我的 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

类 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) 
)

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    感谢 MindVirus,但我想做的事情要简单得多。这只是在我的表单中添加更多 FileFields 的问题,就可以了。这是解决方案: 将当前的 forms.py 更改为:

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

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2013-02-13
      • 2019-03-19
      • 2015-10-04
      • 2010-10-26
      • 2021-08-11
      • 2017-05-03
      • 1970-01-01
      • 2011-04-02
      • 2020-06-17
      相关资源
      最近更新 更多