On Mon, Mar 10, 2008 at 6:09 AM, Paolo Ferretti <[EMAIL PROTECTED]> wrote: > I've got a problem with file upload in django. I found some generic > documentation like this: > > http://www.djangoproject.com/documentation/model-api/#filefield > http://www.djangoproject.com/documentation/faq/#how-do-i-use-image-an...
If you follow the link from the model-api page to the db-api page[1] you'll notice a method designed to do exactly what you need. Try something like this in your views.py: def addphotoauto(request, id): if request.method == 'GET': form = AddImageForm() return render_to_response('auto/aggiungi_foto.html', {'form': form}) if request.method == 'POST': form = AddImageForm(request.POST, request.FILES) if form.is_valid(): try: a = Auto.objects.get(id__exact = id) filename = form.cleaned_data['immagine'].filename content = form.cleaned_data['immagine'].content a.save_foto_file(filename, content) return HttpResponseRedirect('/') except Exception, e: # exception handling else: return render_to_response('auto/aggiungi_foto.html', {'form': form}) Note that you don't need to explicitly save the model after using save_foto_file(), because it does that for you, by default. This whole situation probably does need to be documented a bit better, as you're far from the first person to miss that method. -Gul [1] http://www.djangoproject.com/documentation/db-api/#get-foo-filename [2] http://www.djangoproject.com/documentation/db-api/#save-foo-file-filename-raw-contents --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---