Форум сайта python.su
Здравствуйте! Помогите пожалуйста с отображением формы.
models.py:
class Mydocuments(models.Model): docfile = models.FileField(upload_to='')
class DocumentForm(forms.Form): class Meta: model = Mydocuments fields = ['docfile']
def list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): print(request.POST) newdoc = Mydocuments(docfile = request.FILES['docfile']) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('contact_info.views.list')) else: form = DocumentForm() # A empty, unbound form # Load documents for the list page documents = Mydocuments.objects.all() # Render list page with the documents and the form return render_to_response('list.html' ,{'documents': documents, 'form': form}, context_instance=RequestContext(request))
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Minimal Django File Upload Example</title> </head> <body> {% if documents %} <ul> <img src="{{ documents.docfile.url }}" width=200;/> </ul> {% else %} <p>No documents.</p> {% endif %} <form action="" 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><input type="submit" value="Upload" /></p> </form> </body> </html>
Офлайн