Do you use `enctype` attribute[1] of form tag? [1]: http://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form
On Thu, Nov 20, 2008 at 16:59, Marc Barranco <[EMAIL PROTECTED]> wrote: > > 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/ <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() > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---