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:
- How to pass a tag to the HTML
- How to generate a QR code
- How to use a variable as a stream (to write the SVG code not to the file but to the variable)
- How to use Bootstrap :) Just kidding.
The Tutorial
- pip install django
- pip install qrcode
- 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…