python - Django multiple forms one Submit -
i know question has been answered before on site, life of me can't figure out. want submit 2 forms @ once, one submit button. please, can identify wrong code, driving me insane.
html template
(edited)
<!doctype html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="{% url "list" %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <p>{{ form.non_field_errors }}</p> <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p> <p> {{ form.docfile.errors }} {{ form.docfile }} </p> <p>{{ form.non_field_errors }}</p> <p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p> <p> {{ form.docfile.errors }} {{ form.docfile }} </p> <p><input type="submit" value="upload"/></p> </form> <!-- check error {% if form.errors %} {% field in form %} {% error in field.errors %} <div class="alert alert-error"> <strong>{{ error|escape }}</strong> </div> {% endfor %} {% endfor %} {% error in form.non_field_errors %} <div class="alert alert-error"> <strong>{{ error|escape }}</strong> </div> {% endfor %} {% endif %} --> </body>
views.py
# -*- coding: utf-8 -*- django.shortcuts import render_to_response django.template import requestcontext django.http import httpresponseredirect django.core.urlresolvers import reverse myproject.myapp.models import document myproject.myapp.forms import documentform myproject.myapp.forms import documentform2 def list(request): # handle file upload if request.method == 'post': form = documentform(request.post, prefix="form") form2 = documentform2(request.post, prefix="form2") if form.is_valid() or form2.is_valid(): newdoc = document(docfile=request.files['docfile']) newdoc.save() # redirect document list after post return httpresponseredirect(reverse('myproject.myapp.views.list')) else: form = documentform(prefix="form") # empty, unbound form form2 = documentform2(prefix="form2") # empty, unbound form # load documents list page documents = document.objects.all() # render list page documents , form return render_to_response( 'list.html', {'documents': documents, 'form': form}, context_instance=requestcontext(request) )
forms.py
# -*- coding: utf-8 -*- django import forms class documentform(forms.form): docfile = forms.filefield( label='select file' ) class documentform2(forms.form): docfile2 = forms.filefield( label='select file' )
first, have 2 forms defined in views method form
, form2
, add form
context.
secondly, don't put 2 forms in 2 different <form>
tags. should put them in 1 submit them @ same time.
thirdly, not related, cannot name views method list
. that's django keyword data structure list
, run huge trouble if try use list
in other functions in views file.
edit:
you keep using variable {{ form.something }}
have 2 forms, render first form twice ignored second form. should:
<form action="{% url "list" %}" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} {{ form2.as_p }} <p><input type="submit" value="upload"/></p> </form>
reedit:
i'm not sure caused 403 happen, let's try replace render_to_response
render
:
from django.shortcuts import render def list(request): # code here return render(request, 'list.html', {'documents': documents, 'form': form, 'form2': form2})
Comments
Post a Comment