Learn how to create QR-Code in Django

In Today’s advanced era QR-codes are becoming an essential part of the online business. Therefore in today’s article, we learn to create our own QR-Code based on the model field and also save the QR-Code onto the database.

First, Create a brand new Django project with basic Django settings and open models.py file to create and save the QR-Code on the database.

Learn how to handle ajax request in django

Prerequisite

  1. qrcode python library (to work with qr-code)
  2. Pillow library (to work with Image field and save the qr-code)

Install the above-mentioned libraries in the Powershell using pip

Detailed Video Tutorial is on youtube

$ pip install pillow qrcode

Now open the model.py file and create the model for the project

Models.py

from django.db import models

class GenerateQR(models.Model):
  name = models.CharField(max_length=100)
  qr_image = models.ImageField(blank=True, null=True)
  
  def __str__(self):
    return self.name

Now, the model is complete, make sure to run the migrations.

We will create QR-code based on the name field given and when the save method is called then we will generate the QR-code and save it to the qr_image field. So, let’s implement the save method.

# imports
import qrcode
from django.db import models
from io import BytesIO
from django.core.files import File
from PIL import Image, ImageDraw

class GenerateQR(models.Model):
  name = models.CharField(max_length=100)
  qr_image = models.ImageField(blank=True, null=True)
  
  def __str__(self):
    return self.name
  # save method
  def save(self, *args, **kwargs):
    qr_image = qrcode.make(self.name)
    qr_offset = Image.new('RGB', (310, 310), 'white')
    draw_img = ImageDraw.Draw(qr_offset)
    qr_offset.paste(qr_image)
    file_name = f'{self.name}-{self.id}qr.png'
    stream = BytesIO()
    qr_offset.save(stream, 'PNG')
    self.qr_field.save(file_name, File(stream), save=False)
    qr_offset.close()
    super().save(*args, **kwargs)

In the above code make() method is called to generate the QR-code and the Image.new() defines the property of the QR-code(color and width) and then simply save the file to the desired location.

This is all you have to do to simply generate the QR-code.

Demo and Video Tutorial

Make Sure to Subscribe to Newsletter to get such articles directly in your inbox.

Thanks

Happy Coding

Cheers!!!

This Post Has 3 Comments

  1. som thakur

    error in models.py

    ImproperlyConfigured Traceback (most recent call last)
    in ()
    2 from django.db import models
    3
    —-> 4 class GenerateQR(models.Model):
    5 name = models.CharField(max_length=100)
    6 qr_image = models.ImageField(blank=True, null=True)

    4 frames
    /usr/local/lib/python3.7/dist-packages/django/conf/__init__.py in _setup(self, name)
    65 “You must either define the environment variable %s ”
    66 “or call settings.configure() before accessing settings.”
    —> 67 % (desc, ENVIRONMENT_VARIABLE))
    68
    69 self._wrapped = Settings(settings_module)

    ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

    this error in second code
    File “”, line 6
    from pil import Image ImageDraw
    ^
    SyntaxError: invalid syntax

    thank you please look into code

    1. pydjango

      Register your django app in settings.py file under INSTALLED_APPS list and
      in line 6 it is “from PIL import Image, ImageDraw”

Leave a Reply to som thakur Cancel reply