In this blog tutorial, we will learn how to Generate QR code in Django for any input provided.
How to Generate a QR Code in Django
Generate QR code in Django

How to Generate a QR Code in Django

Step 1: SETUP

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

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

How to Generate a QR Code in Django

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.

How to Generate a QR Code in Django

Now, in order to link the newly generated template and static folder to the project, go to 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'


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

Finally, scroll to the end and add STATIC_DIR to STATICFILES_DIRS


STATIC_URL = '/static/'

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

How to Generate a QR Code in Django

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

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">
    <meta charset="UTF-8">
        <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">
        {% if data %}
        <h4>Your QR Code is:</h4>
            <img src="{% static 'image/test.png' %}">
        {% endif %}

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 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 *
def home(request):
    global data
    if request.method=="POST":
    return render(request,"home.html",{'data':data})

Step3: Modify

Finally let’s modify the file to add the necessary path ‘send’ which redirects to home page upon clicking the submit button. Consequently, the 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('send', views.home),

That finishes the project!

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

How to Generate a QR Code in Django
How to Generate a QR Code in Django

Upon clicking the link, the following will be displayed.

How to Generate a QR Code in Django

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

Generate QA Code in Django
Generate QR code in Django

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