Ticket #17016: model_file_uploads.diff

File model_file_uploads.diff, 1.8 KB (added by tim.saylor@…, 13 years ago)
  • AUTHORS

     
    550550    Gasper Zejn <zejn@kiberpipa.org>
    551551    Jarek Zgoda <jarek.zgoda@gmail.com>
    552552    Cheng Zhang
     553    Tim Saylor <tim.saylor@gmail.com>
    553554
    554555A big THANK YOU goes to:
    555556
  • docs/topics/http/file-uploads.txt

     
    179179        Which means "try to upload to memory first, then fall back to temporary
    180180        files."
    181181
     182Handling uploaded files with a model
     183------------------------------------
     184
     185If you're saving a file from a standard form into a :class:`FileField` on a
     186model, the process is much easier. The :class:`FileField` accepts the parameter
     187'upload_to', which tells the model where to store your file. In the view, you
     188can simply assign the file object from reqeust.FILES to the file field in the
     189model and the file will be saved to disk in the location specified by the
     190'upload_to' parameter::
     191
     192    from django.http import HttpResponseRedirect
     193    from django.shortcuts import render_to_response
     194    from models import ModelWithFileField
     195
     196    def upload_file(request):
     197        if request.method == 'POST':
     198            form = UploadFileForm(request.POST, request.FILES)
     199            if form.is_valid():
     200                instance = ModelWithFileField(file_field=request.FILES['file'])
     201                instance.save()
     202                return HttpResponseRedirect('/success/url/')
     203        else:
     204            form = UploadFileForm()
     205        return render_to_response('upload.html', {'form': form})
     206
    182207``UploadedFile`` objects
    183208========================
    184209
Back to Top