Django Python Generate QrCode Example Tutorial

Published On: 06/07/2022 | Category: Django Python

Hi Dev,

Today, how to generate a qr code in django is our main topic. Here you will learn how to generate a qr code in django and python. you'll learn how to generate a qr code in django example. you can understand a concept of how to generate a qr code in django in python. Let's see bellow example how to generate a qr code in django app.

Here i explained simply step by step example of how to generate qr code in django python.

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 startapp qrcode
Step 3 : Update

In this step we require to do two things in our file, One is to change the path of template look up directory. Second one is to configure our media folder. Add the below lines to your file:

Next, you need to add it in the file as follows:

Step 4 : Install qrcode Library

First of all we can use the qrcode library in Django to convert an qrcode. Let us see how to use this library.

pip install qrcode
Step 5 : Creating the Views

In this step, we need to create the views for performing fetch record to the database.Open the qrcode/ file and add:

from django.shortcuts import render
import qrcode
import qrcode.image.svg
from io import BytesIO

# Create your views here.

def qrCode(request):
    context = {}
    if request.method == "POST":
        factory = qrcode.image.svg.SvgImage
        img = qrcode.make(request.POST.get("qr_text",""), image_factory=factory, box_size=20)
        stream = BytesIO()
        context["svg"] = stream.getvalue().decode()

    return render(request, "index.html", context=context)
Step 6 : Creating the Templates

Next, open the qrcode/templates/index.html file and the add:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <title>QR Code Generator</title>
  <link rel="stylesheet" href="[email protected]/dist/css/bootstrap.min.css">
    <div class="container mt-5 pt-3">
        <div class="row d-flex justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h4>How to Generate QrCode in Django Python? - <span class="text-primary"></span></h4>
                    <div class="card-body">
                        <form method="post">
                            {% csrf_token %}
                            <div class="input-group mb-3">
                                <span class="input-group-text" id="inputGroup-sizing-default">QR Code Text:</span>
                                <input type="text" class="form-control" aria-describedby="inputGroup-sizing-default" name="qr_text" autofocus>
                            <div class="row">
                                <div class="col-md-12 text-center">
                                    <button type="submit" class="btn btn-success">Submit</button>
                        <div class="row">
                            <div class="col-md-12 text-center">
                                {{ svg|safe }}
Step 7 : Creating URLs

In this section, we’ll create the urls to access our qrcode views.Go to the qrcode/ file and update it as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('qr-code/', views.qrCode),

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('qrcode.urls')),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.

I Hope It will help you....