quine
quine

Reputation: 1022

How Can I attach multiple forms to a single submit button in Django?

This is a very noob-ish question but I can't figure it out. I want to use a single submit for 2(or maybe eventually more) forms in my Django template. Here is what I am doing in my template but its obviously not right.

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

My forms.py file looks like this

from django import forms # for UploadFileForm

class DocumentForm(forms.Form): docfile = forms.FileField(label='Select a file', help_text='No limit on file size')

Here is my view

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

Thanks in advance!

Upvotes: 0

Views: 600

Answers (2)

quine
quine

Reputation: 1022

Thanks MindVirus, but what I was trying to do was much simpler. It was just a question of adding more FileFields to my Form & that did the trick. Here is the solution: Change the current forms.py to:

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

Upvotes: 1

Related Questions