Uploading images by Ajax

File uploads using Ajax has become the de facto standard on the web. People want to see what they have chosen right after selecting a file instead of seeing it after submitting a form. Also, if the form has validation errors, nobody wants to select the files again; the file should still be selected in the form with validation errors.

There is a third-party app, django-ajax-uploader, that can be used to upload images with Ajax. In this recipe, we will see how to do this.

Getting ready

Let's start with the quotes app that we created for the Uploading images recipe in Chapter 3, Forms and Views. We will reuse the model and view; however, we'll create a different form and template and add JavaScript too.

Install django-crispy-forms ...

Get Web Development with Django Cookbook - Second 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.