Skip to content
Related Articles

Related Articles

Improve Article
How to integrate Mysql database with Django?
  • 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :