How to Generate a QR Code in Django

In this blog tutorial, we will learn how to generate QR code in Django for any input provided.

Step 1: SETUP

We need to have the following libraries installed for this project:

  • Django
  • image
  • qrcode


The platform we will be using here is PyCharm. The first step is to start the project. Let’s name it qrcode_generator. Run the following code in the terminal to start your project.


django-admin start project qrcode_generator

The next step is to create to two directories: static, and template under qrcode_generator. Right click and click on directories to create them. The Next step is to create a folder “image” under static directory.

Now, in order to link the newly generated template and static folder to the project, go to settings.py and make the following changes. Add “TEMPLATES_DIR” AND STATIC_DIR” like this:

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR = BASE_DIR / 'templates'
STATIC_DIR = BASE_DIR / 'static'

Add TEMPLATES_DIR in TEMPLATES accordingly.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR],
        'APP_DIRS': True,

Finally, scroll to the end and add STATIC_DIR to STATICFILES_DIRS

# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS=[
    STATIC_DIR,]

Step 2:Create a basic front-end for the webapp.

In order to create a front-end to take the input and display the QRCode,. go to the newly created templates directory and create a html file. Let’s call it homepage.html

And in the qrcode_generator folder, create a new python file, “views.py”.

Next step is to create a basic front-end for our web application by modifying the “homepage.html”. In order to do that, we use a form that contains a text field to enter the data, and an image field which should display the qr code image file as follows:

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
    <meta charset="UTF-8">
<title>title</title>
</head>
<body>
    <div>
        <form action="/send" method="post">
            {% csrf_token %}
            <h3 id="h1">Enter Data For Generating QR Code <input type="text" name="data" ></h3>
            <input type="submit" value="SUBMIT">
        </form>
    </div>
    <div>
        {% if data %}
        <h4>Your QR Code is:</h4>
            <img src="{% static 'image/test.png' %}">
        {% endif %}
    </div>
</body></html>

This completes the front-end part .However, the QR code is not generated yet. Now, in order to create the  QR code, let’s change the views.py file. Therefore, we need to import the library called qrcode.

Once the data is submitted in the front-end, the data gets entered in post method. After that, we  take this data into the  “data” variable. make() is the function from the qrcode module which converts the data into the QR code. Let’s save this image as test.png.

from django.shortcuts import render
from qrcode import *
data=None
def home(request):
    global data
    if request.method=="POST":
        data=request.POST['data']
        img=make(data)
        img.save("static/image/test.png")
    else:
        pass
    return render(request,"home.html",{'data':data})

Step3: Modify urls.py

Finally let’s modify the urls.py file to add the necessary path ‘send’ which redirects to home page upon clicking the submit button. Consequently, the views.py function is called with the POST method. Modify the file as follows:

from django.contrib import admin
from django.urls import path
from qrcode_generator import views


urlpatterns = [
    path('admin/', admin.site.urls),
    path('',views.home),
    path('send', views.home),

That finishes the project!

To illustrate, let’s type python manage.py runserver in the terminal to run the webapp and click on the link generated.

Upon clicking the link, the following will be displayed.

Select your preferred text to be converted into a QR code and click submit. Meanwhile, the QR Code will be generated and displayed!

This is it! You have successfully created a Django application which generates QR codes!