The Wayback Machine - https://web.archive.org/web/20240830113502/https://www.geeksforgeeks.org/how-to-integrate-mysql-database-with-django/
Open In App

How to integrate Mysql database with Django?

Last Updated : 07 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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. 

Installation

Let’s first understand how to install MySQL to install the Mysql database you can refer to the link below and install MySQL on your local machine.

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

After downloading install the setup and set the admin and password for mysql. Then, install Django, and mysqlclient, for that you need to run the following command in your terminal.

pip install django
pip install mysqlclient

Steps to connect MySQL to Django

Step 1: Create a new project and start the process to connect django with mysql.

django-admin startproject MyDB

Step 2: Move to the MyDB folder.

cd MyDB

Note: You should have MySQL installed and be able to log in with appropriate privileges to create a database.

Step 3: Once you’re logged into MySQL, you can create a new database. Replace your_database_name with the desired name for your database.

CREATE DATABASE your_database_name

For better understanding you may refer to the article: Create a MySql database.

Step 4: Update the settings.py

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

Python3




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


First, we have replaced the ‘django.db.backends.sqlite3’ to ‘django.db.backends.mysql’. This is basically indicating we shift SQLite to MySQL database.

  • NAME: It indicates the name of the database we want to connect.
  • USER: The MYSQL username is the one who has access to the database and manages it.
  • PASSWORD: It is the password of the database. 
  • HOST: It is indicated by “127.0.0.1” and “PORT” “3306” that the MySQL database is accessible at hostname “0.0.1” and on port “3306.”

Step 5: Run the server.

python manage.py runserver

Step 6: Run the migration command

python manage.py makemigrations

How to connect MySQL to Django

makemigration command

python manage.py migrate
How to connect MySQL to Django

Apply migrations



Similar Reads

How to Integrate Custom Rich Text-Editor in Your Django Website?
In this article, we will implement tinymce text editor in our Django application. What is django-tinymce? django-tinymce is a Django application that contains a widget to render a form field as a TinyMCE editor. Features :- Use as a form widget or with a view.Enhanced support for content languages.Integration with the TinyMCE spellchecker.Enables p
3 min read
Integrate the QuickBooks API with the Python Django
QuickBooks is a powerful accounting software that helps businesses manage their finances efficiently. Integrating the QuickBooks API with a Django application can automate many financial processes, saving time and reducing errors. This article will guide you through the steps required to integrate the QuickBooks API with a Python Django application
6 min read
How to Generate Database Dump of MySQL Database in Linux?
We will learn how to Backup and Restore your MySQL Database with Mysqldump query in LINUX. We had always a need that if the production database is corrupted somehow, we should be able to recover it, for recovering we should always keep a backup of the database at particular instances. So, data rollback can be done. First, we will create a Database
2 min read
Connect MySQL database using MySQL-Connector Python
While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases using MySQL Connector/Python.MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Pytho
2 min read
Adding Tags Using Django-Taggit in Django Project
Django-Taggit is a Django application which is used to add tags to blogs, articles etc. It makes very easy for us to make adding the tags functionality to our django project.Setting up Django Project Installing django-taggit pip install django-taggitADD it to Main Project's settings.py file C/C++ Code INSTALLED_APPS = [ 'django.contrib.admin', 'dja
2 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id
3 min read
Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Modules required:django : django installdjango-crispy
1 min read
Integrating Django with Reactjs using Django REST Framework
In this article, we will learn the process of communicating between the Django Backend and React js frontend using the Django REST Framework. For the sake of a better understanding of the concept, we will be building a Simple Task Manager and go through the primary concepts for this type of integration between React js and Django. Reactjs in a nuts
15+ min read
How to Fix Django "ImportError: Cannot Import Name 'six' from 'django.utils'"?
After Django 3.0, django.utils.six was removed. The "ImportError: Cannot Import Name 'six' from 'django.utils'" error occurs because of the dependency issue. This happens because when the project tries to use something that’s no longer available. Don’t worry—it’s easy to fix. We’ll explain why this error happens and give you simple steps to solve i
3 min read
How to create a new project in Django using Firebase Database?
Django is a Python-based web framework that allows you to quickly create efficient web applications. If you are new to Django then you can refer to Django Introduction and Installation. Here we are going to learn How to create a Django project using Firebase as Database . How to create a new project in Firebase ? Step 1: Firstly, We are going to Cr
3 min read
How to use PostgreSQL Database in Django?
This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are choosing a database for your applications. Also ch
2 min read
Django Transaction System with Database Updates
In today's digital age, transaction systems play a pivotal role in various industries, from e-commerce platforms to banking and beyond. These systems are responsible for handling financial operations, ensuring data accuracy, and providing a seamless user experience. One of the most powerful tools for building transaction systems is Django, a high-l
4 min read
Database Functions in Django
In Django, database functions play an important role in querying, manipulating, and managing data stored in your application's database. In this article, we will learn about database functions in Django. What are Database Functions in Django?In Django, functions such as annotate, aggregate, and filter make it easier to work with data in the databas
4 min read
Different types of Filter Command for Database in Django
Django follows the Model-View-Controller (MVC) architectural pattern, although it’s referred to as Model-View-Template (MVT) in Django terminology: Model: Defines the data structure.View: Manages the logic and interaction.Template: Manages presentation and rendering to the user.How Django's ORM Processes QueriesDjango's ORM allows developers to int
4 min read
How to Clone and Save a Django Model Instance to the Database
In the realm of web development, Django stands out as a robust and versatile framework for building web applications swiftly and efficiently. One common requirement in Django projects is the ability to clone or duplicate existing model instances and save them to the database. This functionality is particularly useful when dealing with repetitive da
3 min read
Integrate Legendre series and set the lower bound of the integral using NumPy in Python
In this article, we will see how to integrate a Legendre series and set the lower bound of the integral in Python using NumPy. To perform Legendre integration, NumPy provides a function called legendre.legint which can be used to integrate Legendre series. Syntax: legendre.legint(c, lbnd=0, scl=1, axis=0) Parameters: c – Array of Legendre series co
2 min read
Python | sympy.integrate() method
With the help of sympy.integrate() method, we can find the integration of mathematical expressions in the form of variables by using sympy.integrate() method. Syntax : sympy.integrate(expression, reference variable) Return : Return integration of mathematical expression. Example #1 : In this example we can see that by using sympy.integrate() method
1 min read
Python | sympy.integrate() using limits
With the help of sympy.integrate(expression, limit) method, we can find the integration of mathematical expressions using limits in the form of variables by using sympy.integrate(expression, limit) method. Syntax : sympy.integrate(expression, reference variable, limit) Return : Return integration of mathematical expression. Example #1 : In this exa
1 min read
Python | Scipy integrate.quadrature() method
With the help of scipy.integrate.quadrature() method, we can get the computation of definite integral using fixed tolerance gaussian quadrature by using scipy.integrate.quadrature() method. Syntax : scipy.integrate.quadrature(func, a, b) Return : Return gaussian quadrature approximation to integral. Example #1 : In this example we can see that by u
1 min read
Python | Scipy integrate.tplquad() method
With the help of scipy.integrate.tplquad() method, we can get the triple integration of a given function from limit a to b by using scipy.integrate.tplquad() method. Syntax : scipy.integrate.tplquad(func, a, b) Return : Return the triple integrated value of a polynomial. Example #1 : In this example we can see that by using scipy.integrate.tplquad(
1 min read
Python | Scipy integrate.dblquad() method
With the help of scipy.integrate.dblquad() method, we can get the double integration of a given function from limit a to b by using scipy.integrate.dblquad() method. Syntax : scipy.integrate.dblquad(func, a, b) Return : Return the double integrated value of a polynomial. Example #1 : In this example we can see that by using scipy.integrate.dblquad(
1 min read
Python | Scipy integrate.quad() method
With the help of scipy.integrate.quad() method, we can get the integration of a given function from limit a to b by using scipy.integrate.quad() method. Syntax : scipy.integrate.quad(func, a, b) Return : Return the integration of a polynomial. Example #1 : In this example we can see that by using scipy.integrate.quad() method, we are able to get th
1 min read
Python | Scipy integrate.romb() method
With the help of scipy.integrate.romb() method, we can get the romberg integration using samples of a function from limit a to b by using scipy.integrate.romb() method. Syntax : scipy.integrate.romb(y, dx, axis, show) Return : Return the romberg integration of a sample. Example #1 : In this example we can see that by using scipy.integrate.romb() me
1 min read
Python | Scipy integrate.fixed_quad() method
With the help of scipy.integrate.fixed_quad() method, we can get the computation of a definite integral using fixed order gaussian quadrature by using scipy.integrate.fixed_quad() method. Syntax : scipy.integrate.fixed_quad(func, a, b, n) Return : Return the computed value of a definite integral. Example #1 : In this example we can see that by usin
1 min read
Python | Scipy integrate.romberg() method
With the help of scipy.integrate.romberg() method, we can get the romberg integration of a callable function from limit a to b by using scipy.integrate.romberg() method. Syntax : scipy.integrate.romberg(func, a, b) Return : Return the romberg integrated value of a callable function. Example #1 : In this example we can see that by using scipy.integr
2 min read
Python | Scipy integrate.simps() method
With the help of scipy.integrate.simps() method, we can get the integration of y(x) using samples along the axis and composite simpson's rule by using scipy.integrate.simps() method. Syntax : scipy.integrate.simps(y, x) Return : Return the integrated value of y(x) using samples. Example #1 : In this example we can see that by using scipy.integrate.
1 min read
Integrate a Hermite_e series Over Axis 0 using Numpy in Python
In this article, we will cover how to integrate a Hermite_e series over axis 0 using NumPy in Python. NumPy e.hermeint() method We use the hermite e.hermeint() function present in the NumPy module of python to integrate a Hermite e series. The first parameter 'arr' is an array of coefficients from the Hermite e series. If 'arr' is multi-dimensional
3 min read
Python Program to Integrate a Chebyshev Series and Set the Integration Constant
In this article, we will discuss how to integrate a Chebyshev Series and set the integration constant To perform Chebyshev integration, NumPy provides a function called chebyshev.chebint which can be used to integrate Chebyshev series. Syntax: chebyshev.chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0) Parameters: c - Array of Chebyshev series coefficie
3 min read
Python Program Integrate a Chebyshev Series and Set the Lower Bound of the Integral
The Chebyshev series has polynomials with the largest possible leading coefficient, whose absolute value on the interval [−1, 1] is bounded by 1. They are also the "extremal" polynomials. Chebyshev polynomials are significant in approximation theory because the roots of Tn(x), which are also called Chebyshev nodes, are used as matching points for o
3 min read
Integrate a Chebyshev series and set the order of integration using NumPy in Python
In this article, we will discuss how to integrate the Chebyshev series and set the order of integration in Python and NumPy. chebyshev.chebint method Chebyshev polynomials are significant in approximation theory because the Chebyshev nodes are used as matching points for optimizing polynomial interpolation. To perform Chebyshev Integration, NumPy p
4 min read
Practice Tags :