Ticket #17016: 17016.diff

File 17016.diff, 2.5 KB (added by Tim Graham, 12 years ago)
  • AUTHORS

    diff --git a/AUTHORS b/AUTHORS
    index 6ca8b29..0ea0ca6 100644
    a b answer newbie questions, and generally made Django that much better:  
    467467    Vinay Sajip <vinay_sajip@yahoo.co.uk>
    468468    Bartolome Sanchez Salado <i42sasab@uco.es>
    469469    Kadesarin Sanjek
     470    Tim Saylor <tim.saylor@gmail.com>
    470471    Massimo Scamarcia <massimo.scamarcia@gmail.com>
    471472    Paulo Scardine <paulo@scardine.com.br>
    472473    David Schein
  • docs/topics/http/file-uploads.txt

    diff --git a/docs/topics/http/file-uploads.txt b/docs/topics/http/file-uploads.txt
    index 877d3b4..4cfc9e2 100644
    a b Three settings control Django's file upload behavior:  
    178178    Which means "try to upload to memory first, then fall back to temporary
    179179    files."
    180180
     181Handling uploaded files with a model
     182------------------------------------
     183
     184If you're saving a file on a :class:`~django.db.models.Model` with a
     185:class:`~django.db.models.FileField`, using a :class:`~django.forms.ModelForm`
     186makes this process much easier. The file object will be saved when calling
     187``form.save()``::
     188
     189    from django.http import HttpResponseRedirect
     190    from django.shortcuts import render_to_response
     191    from forms import ModelFormWithFileField
     192
     193    def upload_file(request):
     194        if request.method == 'POST':
     195            form = ModelFormWithFileField(request.POST, request.FILES)
     196            if form.is_valid():
     197                # file is saved
     198                form.save()
     199                return HttpResponseRedirect('/success/url/')
     200        else:
     201            form = ModelFormWithFileField()
     202        return render_to_response('upload.html', {'form': form})
     203
     204If you are constructing an object manually, you can simply assign the file
     205object from :attr:`request.FILES <django.http.HttpRequest.FILES>` to the file
     206field in the model::
     207
     208    from django.http import HttpResponseRedirect
     209    from django.shortcuts import render_to_response
     210    from models import ModelWithFileField
     211
     212    def upload_file(request):
     213        if request.method == 'POST':
     214            form = UploadFileForm(request.POST, request.FILES)
     215            if form.is_valid():
     216                instance = ModelWithFileField(file_field=request.FILES['file'])
     217                instance.save()
     218                return HttpResponseRedirect('/success/url/')
     219        else:
     220            form = UploadFileForm()
     221        return render_to_response('upload.html', {'form': form})
     222
     223
    181224``UploadedFile`` objects
    182225========================
    183226
Back to Top