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 manage.py startapp qrcode
Step 3 : Update setting.py

In this step we require to do two things in our settings.py 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 settings.py file:

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

....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'qrcode',
]
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/views.py file and add:

qrcode/views.py
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()
        img.save(stream)
        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">
<head>
  <meta charset="UTF-8">
  <title>QR Code Generator</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
    <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">Tuts-Station.com</span></h4>
                    </div>
                    <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>
                            <div class="row">
                                <div class="col-md-12 text-center">
                                    <button type="submit" class="btn btn-success">Submit</button>
                                </div>
                            </div>
                        </form>
                        <div class="row">
                            <div class="col-md-12 text-center">
                                {{ svg|safe }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
Step 7 : Creating URLs

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

qrcode/urls.py
from django.urls import path
from . import views

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

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

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    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 manage.py runserver

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

I Hope It will help you....