Client side image zooming and rotating in Django

PythonServer Side ProgrammingProgramming

Sometimes, we may need to rotate an image or zoom it. In such cases, a client-side image zooming library is used to add jquery feature of zooming-rotating on html file. We just needed to load jquery js and css in html file.

Create a Django project and an app. Setup urls and do some basic stuff like adding app in INSTALLED_APPS.

Install the django-client-side-image-cropping library

pip install django-client-side-image-cropping

Now in, add the following line −


Here, we have done a simple setting of adding a module as an app.


In, add the following lines −

from django.db import models

# Create your models here.
class Data(models.Model):

   image=models.ImageField(upload_to ='myimages/')

Here, we setup our model which have an image field.

Setup media root in

In, add the following lines −

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
   path('', views.home,name="home"),
urlpatterns += static(settings.MEDIA_URL,

Here, we have done a basic media url setting which you surely know.

Now in, add the following −

from django.shortcuts import render
from django import forms
from .models import Data
from client_side_image_cropping import ClientsideCroppingWidget

# Create your views here.
class DataForm(forms.ModelForm):
   class Meta:
         widgets = {
            'image': ClientsideCroppingWidget(

def home(request):
   if request.method =="POST":
      if form.is_valid():
   return render(request,'home.html',{"form":form})

Here, we created a simple form. It is a model form, so on its image field, we have added a cropping widget.

In home.html, add the following −

{% load static %}
{% load cropping %}
{% load thumbnail %}
<!DOCTYPE html>
      <link rel="stylesheet" href="{% static "client_side_i
mage_cropping/cropping.css" %}"></script>
      <link rel="stylesheet" href="{% static "client_side_i
mage_cropping/cropping_widget.css" %}"></script>
      <script src="
      <script src="{% static "client_side_image_cropping/cr
oppie.min.js" %}"></script>
      <script src="{% static "client_side_image_cropping/cr
opping_widget.js" %}"></script>
         {% csrf_token %}
         <form method="post" enctype="multipart/formdata">
            {% csrf_token %}
            {{ form.as_p }}
            <input type="submit" value="Save"/>

At the frontend, we loaded some jQueries and some styles, and render the form which will take the input.

Now all is done. Let's check the output.


You will get an interface like this, on which you can rotate and zoom the photo. It is not a very good looking interface, however you can modify it by using CSS.

Note that the image uploaded might look displaced, so use rotate to set it right. That displacement maybe caused due to browser problems.

Updated on 25-Aug-2021 12:50:15