Django Python Generate QrCode Example Tutorial

Published on March 25, 2025 Python Django

Django Python Generate QR Code Example Tutorial

Hi Dev,

Are you looking to generate a QR code in Django using Python? In this tutorial, you'll learn step-by-step how to create a QR code generator app in Django. This guide uses the qrcode library to render dynamic QR codes directly in your web interface. By following this guide, you’ll understand the full workflow for integrating QR code functionality into a Django project.

Below is a simple and clear example to help you implement QR code generation in your Django Python project.

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 two changes in our settings.py file: first, update the template directory path, and second, configure our media folder. Add the following 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

We use the qrcode Python library to generate QR codes. Install it using pip:

pip install qrcode
Step 5 : Creating the Views

Create the view logic to handle QR code generation. Open qrcode/views.py and add the following code:

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

Create an HTML template to accept input and display the generated QR code. Save this file as qrcode/templates/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>QR Code Generator</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/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">Stuffcoder.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

Create a URL route for the QR code view. First, update qrcode/urls.py:

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

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

Then include the app routes in your root project’s example/urls.py 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

Run the development server to test your QR code generator locally:

python manage.py runserver

Now, visit http://localhost:8000/ in your browser to generate QR codes dynamically!


FAQs - Frequently Asked Questions

1. What is the purpose of the qrcode library in Django?

The qrcode library allows Django apps to generate QR codes from text data, URLs, or other input strings. It’s useful for adding scannable codes to websites, invoices, or digital passes.

2. Can I generate QR codes as images instead of SVG?

Yes, the qrcode library supports different output formats including PNG. You can switch from SvgImage to default image format and save it using Django’s FileResponse.

3. Is this QR code generator mobile responsive?

Yes, thanks to Bootstrap’s grid and form layout, the interface works well on both desktop and mobile devices.

4. How do I customize the size or color of the QR code?

You can pass additional parameters like box_size, border, and more in the qrcode.make() method. For colors, use qrcode.QRCode(...).make_image(fill_color="black", back_color="white").

5. Is this production ready?

This is a simple example for learning purposes. For production, consider securing input validation, error handling, and file caching for performance.


I hope this tutorial helps you implement QR code generation in your Django application. Happy Coding!

Related Posts