How to design Student QRcode meal card system using Django

Viewed 40

Hello Django developers, I want to build student cafeteria system using QRcode scanner. Here is how it should work. Student scann there meal card, then the system displays student info and also it should mark served. Then the system count the number of served students. The system should ignore or show red labeled text if there is rescanning.

So here i who can help me what things i have to consider to get the django model.

Thank you

1 Answers

pypi package: qrcode

from django.db.models.signals import pre_save
import qrcode
from django.core.files import File
from io import BytesIO

class Qrcode(models.Model):
    qrcode = models.ImageField(upload_to="images/", blank=True)

def pre_save_qrcode(sender, instance, *args, **kwargs):
    qr = qrcode.QRCode(
        version=15,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=4,
        border=2,
    )
    qr_code_data = f'{instance.data_you_want_to_put.like s.id_card_no}'
    qr.add_data(qr_code_data)
    qr.best_fit(1)
    qr.make(fit=False)
    img = qr.make_image(fill_color="black", back_color="white")
    file_name = 'qr-' + instance.id_card_no + '.png'
    buffer = BytesIO()
    img.save(buffer, 'PNG')
    instance.qrcode.delete(save=False)  # delete previous image from media folder
    instance.qrcode.save(file_name, File(buffer), save=False)
    img.close()


pre_save.connect(pre_save_qrcode, sender=Qrcode)

Here we are using pre_save signal before the model save. Note: version, box_size, border are very important here. box_size and border will increase & decrease size the qr code image for fit.

Related