Hi!
I'm trying to upload a file like the example in documentation in
Django (
http://docs.djangoproject.com/en/dev/topics/http/file-
uploads/)
with the "Forms from Models".
But I can't upload the file because the form always return the error
"this field is required" although selecting a file before submitting
the form.
The fact is that I can do it with the django admin site, but with the
the my own form I don't find where's the problem.
Here is the code:
-------models.py-----------------
from django import forms
from django.forms import ModelForm
from django.db import models
class UploadFile(models.Model):
title = models.CharField(max_length=50)
file = models.FileField(upload_to="pdf/")
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
--------views.py---------------
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from llunes.files.models import *
def upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file']) # <--In fact it never
entries here
#'cause it seems that the
#form is not valid
return HttpResponseRedirect('/files/upload/')
else:
form = UploadFileForm()
return render_to_response('files/upload.html', {'form': form})
def handle_uploaded_file(f):
destination = open('tmp.pdf', 'wb+')
for chunk in f.chunks():
destination.write(chunk)
destination.close()