How to Upload and Display Image in Django using ImageField

Learn how to Upload and Display Images in Django and store it in model using ImageField. You can upload multiple images using this Django Tutorial.

For uploading any image or file in Django, we need to install Pillow

pip install Pillow

So the first step is add bellow code in of your django project –

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'), ) 
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/' 
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

And in file add the configurations –

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
     # Project url patterns…

if settings.DEBUG:
     urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

How to Upload and Display Image in Django Model using ImageField

In this example, we will take a example Employee User Image Profile. We will upload Employee Profile Image in Django Model. Bellow is the Employee Django Model.

class Employee(models.Model):
    name = models.CharField(max_length=50)
    emp_image = models.ImageField(upload_to='upload/')

Note – All the uploaded images will be store in media/upload folder.

Our Upload Image look like this –

from django import forms 
from .models import Employee

class EmployeeForm(forms.ModelForm): 
    class Meta: 
        model = Employee 
        fields = ['name', 'emp_image'] 

Under templates directory, emp_image.html looks like this –

<!DOCTYPE html> 
<html lang="en"> 
    <meta charset="UTF-8"> 
    <title>image upload example</title> 
    <form method = "post" enctype="multipart/form-data"> 
        {% csrf_token %} 
        {{ form.as_p }} 
        <button type="submit">Upload</button> 

Whenever, we upload images or any file, we should add this in form tag – enctype="multipart/form-data" and {% csrf_token %}.

Our load the EmployeeForm and render in emp_image.html

from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.views.generic import TemplateView
from employee.forms import EmployeeForm

class EmployeeImage(TemplateView):
    form = EmployeeForm
    template_name = 'emp_image.html'

    def post(self, request, *args, **kwargs):
        form = EmployeeForm(request.POST, request.FILES)
        if form.is_valid():
            return HttpResponseRedirect(reverse_lazy('home', kwargs={'pk': pk}))
        context = self.get_context_data(form=form)
        return self.render_to_response(context)     

    def get(self, request, *args, **kwargs):
        return, *args, **kwargs)

Display Uploaded Image in Django HTML

It is very easy to Display in Image in Django Template. Here is emp_image_display.html file

{% load static %}
<!DOCTYPE html>
    <h1>Name - {{}}</h1>
    <img src="{{emp.emp_image.url}}" alt="Smiley face" width="250" height="250">
    <br />
    <a href="{% url 'home' %}">Go Back!!!</a>

And in, we are using DetailView to Display Image.

from django.views.generic import DetailView
from employee.models import Employee

class EmpImageDisplay(DetailView):
    model = Employee
    template_name = 'emp_image_display.html'
    context_object_name = 'emp'

That’s it. We can Upload Image and Display Image in Django.

Find this tutorial on Github.