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.

CREATE DATABASE paperbun;

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 settings.py file. This setting specifies the database settings that Django should use. Here’s an example of how to set Django up to use MySQL:

DATABASES = {
    '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 manage.py startapp appname

To proceed further, we need to create the models in the models.py 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 manage.py makemigrations
python manage.py 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 views.py 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.

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Designation</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        {% for employee in employees %}
        <tr>
            <td>{{ employee.name }}</td>
            <td>{{ employee.designation }}</td>
            <td>{{ employee.salary }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

To access this URL from the browser, we need to add this URL pattern in the urls.py 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: