Ticket #5386: newforms.content_type.diff

File newforms.content_type.diff, 1.8 KB (added by Ionut Ciocirlan <ionut.ciocirlan@…>, 7 years ago)
  • django/newforms/fields.py

     
    416416
    417417class UploadedFile(StrAndUnicode):
    418418    "A wrapper for files uploaded in a FileField"
    419     def __init__(self, filename, content):
     419    def __init__(self, filename, content, content_type):
    420420        self.filename = filename
    421421        self.content = content
     422        self.content_type = content_type
    422423
    423424    def __unicode__(self):
    424425        """
     
    445446        elif not data and initial:
    446447            return initial
    447448        try:
    448             f = UploadedFile(data['filename'], data['content'])
     449            f = UploadedFile(data['filename'], data['content'], data['content-type'])
    449450        except TypeError:
    450451            raise ValidationError(self.error_messages['invalid'])
    451452        except KeyError:
  • docs/newforms.txt

     
    13401340                            client.
    13411341                           
    13421342    ``content``             The array of bytes comprising the file content.
     1343                           
     1344    ``content_type``        The Content-Type of the file as submitted by the
     1345                            browser.
    13431346    ======================  ====================================================
    13441347
    13451348The string representation of an ``UploadedFile`` is the same as the filename
    13461349attribute.
    13471350
     1351You can perform basic validation against the ``content_type`` if you need to
     1352allow only uploads of specific files. Note that this is however unreliable,
     1353since the data is submitted by the browser.
     1354
    13481355When you use a ``FileField`` on a form, you must also remember to
    13491356`bind the file data to the form`_.
    13501357
Back to Top