Uploading a file to Django

April 21, 2015 | Posted in Django | Ajoy Oommen

Though I am writing this post under Django, most of it is general and can be used on any server.

Uploading a file is a two-stage process. The first step is on the browser where the user selects a file and uploads it. The upload can be easily handled with a form’s POST method. An input with type='file' will provide the interface to select the file. It should be noted that the form’s enctype must be multipart/form-data. An example:

<form action="/attach" method="POST" enctype="multipart/form-data">
  <input type="file" name="attachment" />
  <button type="submit">Submit</button>
</form>

When the user clicks on submit, the form is submitted with the selected file to the webserver. If your server is Django, you can easily retrieve this file as:

attachment = request.FILES.get("attachment", None)

Once you get this you can save it locally or upload it to another server. To save the file locally, according to the docs:

def handle_uploaded_file(f):
    with open('some/file/name.txt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

handle_uploaded_file(attachment)

Or if you want to attach it to an email with Mailgun:

files = [("attachment", attachment), ]

def send_complex_message():
    return requests.post(
        "https://api.mailgun.net/custom/api/url",
        auth=("api", "key-7777777777777777777777777777"),
        files=files,
        data={"from": "Excited User <excited@example.com>",
                  "to": "foo@example.com",
                  "subject": "Hello",
                  "html": "<html>HTML version of the body</html>"})
Python Django

Related Posts

27 Feb 2017 » Provisioning an ubuntu server for Django, Postgres, NGINX

15 Jul 2016 » Using Signals in Django

25 Mar 2015 » Creating a blog with Django

14 Sep 2014 » Migrating databases with South

02 Mar 2013 » Using python to complete IBC