Django is a Python web framework that makes building web apps fast. With Django, we can use different database technologies to store the data.

In this article, We’ll see you how to configure Django for MySQL, a popular open-source relational database.

Installing the pre-requisites

To proceed with this article, you need to install python3 & Django on your PC.

-> Install the python by visiting the following URL

Download & Install python

Use the following command to install Django

pip install Django

You also need to install MySql & MySqlclient in your PC.

-> Download the MySQL Installer from the official website

Download & Install MySQL

Use the following command to install mysqlclient

pip install mysqlclient

Setting up MySql

After installing MySql, open the command prompt and enter the following command to the login to the MySQL.

mysql -u root -p

Once you are logged in, run the following command to set up a password for the root user:

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';

Create a database using the MySQL the command line interface.


Configuring the django to use MySQL:

The next step is to configure Django to use the newly created MySQL database. To accomplish this, edit the DATABASES setting in your Django project’s file. This setting specifies the database settings that Django should use. Here’s an example of how to set Django up to use MySQL:

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'paperbun',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',

Please remember to replace the ‘root‘ & ‘root‘ with the username and password that has been set in your MySQL environment.

Create a Django project

Please use the following command to create a Django project

django-admin startproject paperbundemo
cd paperbundemo
python startapp appname

To proceed further, we need to create the models in the file of your app.

from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=100)
    designation = models.CharField(max_length=100)
    salary = models.FloatField()

After creating the models, run the following command to create the database tables based on the models.

python makemigrations
python migrate

Now, we need to add the appname to the installed apps list.

INSTALLED_APPS = [    ...    'appname',    ...]

To display the table, we need to create a view in the file. See the following example for more information on this.

from django.shortcuts import render
from .models import Employee

def display_employees(request):
    employees = Employee.objects.all()
    return render(request, 'employee.html', {'employees': employees})

To Display the data, create the employee.html in the `templates` folder of your app.

        {% for employee in employees %}
            <td>{{ }}</td>
            <td>{{ employee.designation }}</td>
            <td>{{ employee.salary }}</td>
        {% endfor %}

To access this URL from the browser, we need to add this URL pattern in the file of the app.

from django.urls import path
from . import views

urlpatterns = [
    path('employees/', views.display_employees, name='display_employees'),

To test, run the development server & go to http://localhost:8000/employees/ to see the table.

Finally, in this article, we have seen how to setup MySQL along with the Django.

Categorized in:

Tagged in: