Sitemap
Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Member-only story

How to Generate a QR Code in Django

3 min readApr 26, 2021

--

Passing a QR code as an SVG code

This tiny project will generate a QR code for any text and pass it to the HTML template as an SVG code.

SVG is a very convenient medium for URLs or other text. This small demo demonstrates a few things:

  1. How to pass a tag to the HTML
  2. How to generate a QR code
  3. How to use a variable as a stream (to write the SVG code not to the file but to the variable)
  4. How to use Bootstrap :) Just kidding.

The Tutorial

  1. pip install django
  2. pip install qrcode
  3. Create a Django project and an app inside it. We will not need any models here.

We want the generated QR code to be shown at a root URL, i.e., when we type www.yourdomain.com, so we need to add the following URL in urls.py:

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

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index)
]

Sure we need this views.index function now.

Go to your_app/views.py file and write this:

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

--

--

Responses (1)