How to do it...

Let's refine the form for inspirational quotes so that it can support Ajax uploads, using the following steps:

  1. First of all, add the following to your settings:
# settings.py or config/base.py# ...UPLOAD_URL = f'{MEDIA_URL}upload/'UPLOAD_ROOT = os.path.join(MEDIA_ROOT, 'upload')
If you want, you can also update the .gitignore file under the MEDIA_ROOT to avoid committing anything to the UPLOAD_ROOT, just by adding /upload/ as a new line.
  1. Then, in the quotes app, we will define a custom file storage system for uploads using the new setting:
# quotes/storages.pyfrom django.conf import settingsfrom django.core.files.storage import FileSystemStorageupload_storage = FileSystemStorage(location=settings.UPLOAD_ROOT)
  1. Getting ...

Get Django 2 Web Development Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.