Skip to content
Related Articles

Related Articles

How to integrate Mysql database with Django?

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 Mar, 2021

Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. 

How to integrate Mysql database with Django ?

Install Mysql database

https://dev.mysql.com/downloads/installer/

After downloading install the setup and set the admin and password.

Install Django

pip install django

Then install another library to use mysql database

pip install mysqlclient

Create new project

django-admin startproject MyDB
cd MyDB

settings.py

Python3




DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': 'admin',
        'HOST':'localhost',
        'PORT':'3306',
    }
}


Open settings.py here inside the DATABASES variable configure mysql database values, and add values of your database.

python manage.py migrate

Then create new app

python manage.py startapp main

main/models.py

Python3




from django.db import models
from django.contrib.auth.models import User
  
#Create your models here.
class PublishedArticle(models.Model):
    todo = models.CharField(max_length=40)
    date = models.DateField(auto_now=True)


python manage.py migrate

The published article table would have been created in the Database. Hence, Mysql database is successfully integrated


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!