How to Generate Barcode in Django?

Published On: 05/08/2022 | Category: Django

Hi Dev,

This is a short guide on django generate barcode. We will look at example of how to generate barcode in django. you can understand a concept of how to create barcode in django. we will help you to give example of how to save generated barcode in django.

In this example, we will generate a barcode using python-barcode package. I will give you a very simple example of generating Barcode with django.

Let's see the below step and you can generate bar code in your django projects as well.

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 startapp core
Step 3 : Update

In this step we require to do two things in our file, one is to configure our media folder. Add the below lines to your file:

Next, you need to add it in the file as follows:


STATIC_URL = '/static/'
MEDIA_URL = '/media/'
Step 4 : Install python-barcode Library

In this step next, we’ll use a python library for generating barcode called python-barcode as well.As the barcode will be generated in the form of images we’ll make use of the Pillow library as well.

pip install python-barcode
pip install Pillow
Step 5 : Database Setup

Next step, we will modify the file and update the database settings to configure the mydb database:
    'default': {  
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': 'example',  
Step 6: Create a Model

In this step we will require the database model for storing contacts.Open the cropper/ file and add the following code:

from django.db import models
import barcode
from barcode.writer import ImageWriter
from io import BytesIO
from django.core.files import File

class Customer(models.Model):
    name = models.CharField(max_length=50)
    barcode = models.ImageField(upload_to='barcodes/', blank=True)

    def __str__(self):

    def save(self, *args, **kwargs):
        COD128 = barcode.get_barcode_class('code128')
        rv = BytesIO()
        code = COD128(f'{}', writer=ImageWriter()).write(rv)'{}.png', File(rv), save=False)
        return super().save(*args, **kwargs)

After creating these model, you need to create migrations using the following command:

Step 7 : Create a Migrations
python makemigrations

After successfully run the above command go to the cropper/migrations/

# Generated by Django 3.2.6 on 2022-08-04 07:34

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [

    operations = [
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50)),
                ('barcode', models.ImageField(blank=True, upload_to='barcodes/')),

Next, you need to migrate your database using the following command:

python migrate
Step 8 : Update the File

So, in this step let’s register the model in the admin site and test it out. in we add the following lines.

from .models import Customer
Step 9 : Creating URLs

Next, we will require the modify the your root preoject folder lets update the file.

from django.conf import settings
from djano.conf.urls.static import static

urlpatterns = [
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the address with a web browser.


I Hope It will help you....