One of the most appealing features of a modern software is its ability to accommodate images.

Images such as pictures, illustrations, and animations bring visual appeal to an utility.

While Images are important, they can slow down the app and increase security threats to the database.

Django framework

For applications built on Django, uploading images is easy and secure.

Django has a specialized feature that provides for image uploads.

Let’s learn how to upload images to a Django app without compromising security.

Image shows imagefield added to model in admin panel

To installpillowuse the command below:

If you are using venv, use this command instead

2.

Create Model

Create anImageFieldreference in the database.

Then, add theupload_toargument in the model.

The argument defines a storage location in the file system.

The method at the end helps to convert the data into strings.

Next, migrate and commit the new changes to the database.

Then, you should probably edit the project parameters.

Add Media URL to Project prefs

Navigate to the project setting.

Under the heading# Static files (CSS, JavaScript, Images), add the media URL.

Adding media URLs to configs defines a specific route to display uploaded images.

The media file stores the app images.

The path will look like this:127.0.0.1:8000/media/profile/image.jpg

Update theTEMPLATESarray in the project prefs.

The processors' media setting helps to load the uploaded images in the app templates.

Add Media Root to URLs

Next, you better add theMEDIA_ROOTroute to the App URLs.

This will help to load the uploaded images to the development server.

First, import the project configs from thedjango.confmodule and astatic function.

Then, add to theurlpatternsa static route showing the uploaded files' location.

Display the Uploaded Image

You should update the profile template to display the profile image.

You will add animgtag and populate it with theprofile_pictureattribute.

TheImageFieldhas a URL attribute that provides the absolute URL to the file.

you might specify the shape and appearance of the image using CSS classes.

you’ve got the option to launch the server to load the image.

Then check the template on the net surf tool to see the image displayed.

Django has a specialized field on its models that adds and checks for file jot down before uploading.

The ImageField provides an absolute path to a file system to store the images.