๐ป File Upload with Django and DRF ๐ป
Posted on Jul 28th, 2022
๐๏ธ Todayโs Topics
- How are the projects coming along? ๐
- Letting users upload files/images using AWS S3
๐ฏ Project
Keep on going. ๐ช ๐
Today letโs get you past any blockers you may be experiencing and talk through next steps.
By now you should have provided your front end with a way to log in and log out, and endpoints to see at least some data. You can create some data via the Django admin so that you can see your GET requests working. By tomorrow you should be able to create (at least some)resources via POST requests.
๐ Read | ๐บ Watch | ๐ง Listen
File Upload
- Django File (and Image) Uploads Tutorial -> A good and very recent post from Will Vincent; he does not include all the necessary info to make file uploads work in production but otherwise itโs a good overview.
- ๐ File Upload with DRF
- ๐ง + ๐ Success with Static Files
- ๐ What is Amazon S3? -> Skim this โ this is Amazonโs documentation and it gets really in-depth.
- ๐บ Introduction to S3 -> Also from Amazon
Search
On Monday weโll be covering full-text search in your API, but if you have time you can check these resources out in advance.
- Search from the Ground Up -> DjangoCon 2019 video explaining search in detail
- ๐ Basic and Full-Text Search with Django and Postgres
- ๐ Blog post with more on full-text search
- ๐ If you want A LOT more detail about full-text search in Postgres and Django, this blog piece has you covered
๐ Resources
File uploads
- Django Docs: ImageField
- Django Docs: FileField
- Pillow: Python Imaging Library
django-imagekit
-> If you want to resize images when they are uploaded, or do any kind of image processing, you will need this. Donโt add it unless you know you need it.
- How to Set Up Amazon S3
django-storages
- DRF
FileUploadParser
A request with a file attachment using Insomnia
- Select the right HTTP method for your endpoint.
- Choose binary file attachment from the JSON menu (where you normally put the body of a request)
-
Set headers on the Headers tab (this example assumes an image file in jpeg format, named
profile-photo.jpg
):Content-Type: image/jpeg Content-Disposition: attachment; filename=profile-photo.jpg
For more information on the values for Content-Type
:
CORS for file upload
Assuming you are using django-cors-headers
, youโll need to add the following to settings.py
to allow the request headers necessary for file attachments:
# in settings.py
from corsheaders.defaults import default_headers
CORS_ALLOW_HEADERS = list(default_headers) + [
'content-disposition',
]
Authentication
Make sure you are sharing the Djoser information with your front end. You should include the authentication endpoints in your API documentation or project README.
- Base Endpoint Guide for Djoser -> includes create a new user and other nice stuff
- Token Authentication Endpoint Guide for Djoser -> details on the token auth endpoints
Creating a properly hashed password
In order to save a properly hashed password when you create a new user in the Django Admin, make sure you are using UserAdmin
in admin.py
so that you have that option in the admin interface. If you donโt do this and save an unhashed password, you will run into authentication errors.
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
...
admin.site.register(User, UserAdmin)
You can also change a password from the command line: