

{"id":52888,"date":"2019-03-29T15:00:53","date_gmt":"2019-03-29T09:30:53","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=52888"},"modified":"2021-06-21T12:19:38","modified_gmt":"2021-06-21T06:49:38","slug":"django-database","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-database\/","title":{"rendered":"Django Database &#8211; How to Connect MySQL Database with Django Project"},"content":{"rendered":"<p>In the previous tutorial, we learned the <a href=\"https:\/\/data-flair.training\/blogs\/django-models\/\"><strong>concept of Django Models<\/strong><\/a>, now, we will discuss the process to connect database with Django project.<\/p>\n<p>Whenever we are creating a web project or any kind of project, we want some kind of input by our end-users or consumers. All that data\/ input is handled by a Database. In today\u2019s scenario, whenever we are developing a website, we will need a database, whether it\u2019s a blog site or highly interactive ones like Instagram which is based on Django.<\/p>\n<p>To achieve that you would need some software, which can store that data efficiently and also some middleware which can let you communicate with the database.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52928\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg\" alt=\"Django Database\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>Connecting Databases with Django Project<\/h2>\n<p>By default, when we made our first app and started the server you must have seen a new file in your project directory, named as \u2018db.sqlite3\u2019. The file is database file where all the data that you will be generating will be stored. It is a local file as <strong><a href=\"https:\/\/data-flair.training\/blogs\/django-tutorial\/\">Django is a server-side framework<\/a><\/strong> and it treats your computer as the host when you actually run the server in command line\/terminal.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Root-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52909\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Root-Directory.png\" alt=\"Django Database - Root Directory\" width=\"266\" height=\"132\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Root-Directory.png 266w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Root-Directory-150x74.png 150w\" sizes=\"auto, (max-width: 266px) 100vw, 266px\" \/><\/a><\/p>\n<p>This file is generated automatically because Django has a default setting of the database set to the SQLite, which is although fine for testing and provides lots of features but if you want your website to be scalable, you need to change it to any other efficient database.<\/p>\n<p>In our case, we will be using MySQL and this tutorial will help you to integrate your project with MySQL.<\/p>\n<p>In the<strong><a href=\"https:\/\/data-flair.training\/blogs\/install-django\/\"> Django Installation tutorial<\/a><\/strong>, we installed Xampp, it\u2019s a great tool and is a prerequisite for MySQL.<\/p>\n<p>Don\u2019t worry you won\u2019t need to learn SQL for that, all the backend code will be done in python, that is the advantage of Django Model.<\/p>\n<h3>1. DATABASES Dictionary Indexes<\/h3>\n<p>Firstly, open the settings.py file of your web-application\/ project and there find this part.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52910\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py.png\" alt=\"Django Database - root settings.py\" width=\"965\" height=\"325\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py.png 965w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py-150x51.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py-300x101.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py-768x259.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/root-settings-py-520x175.png 520w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><\/a><\/p>\n<p>This partition has information regarding the connection to the database.<\/p>\n<p>DATABASES is a pre-defined dictionary in Django Framework with the \u2018default\u2019 as an index having the value for the main database where all the data is to be stored.<\/p>\n<p>There can be multiple databases as we need data backups too but there is only one default database, although we won\u2019t be adding multiple databases now.<\/p>\n<p>The default is holding a dictionary where there are 2 indexes:<\/p>\n<ul>\n<li><strong>ENGINE<\/strong><\/li>\n<\/ul>\n<p>It specifies the library to be used when connected to a certain website. In the value, we have to put the file, \u201cdjango.db.backends.sqlite3\u201d, which is the <strong><a href=\"https:\/\/data-flair.training\/blogs\/python-libraries\/\">python library<\/a><\/strong> for sqlite3 database and will translate your python code to the database language.<\/p>\n<p>Thus, you won\u2019t need to learn any new database language, every code is in Python.<\/p>\n<ul>\n<li><strong>NAME<\/strong><\/li>\n<\/ul>\n<p>Here you will have a name of the database that you are using and the location of your database. This parameter changes according to the type of database you are using. Here you can experiment with the database file.<\/p>\n<p>In this, we are also passing the name of the database file or if the file is not present, this will create the db.sqlite3 file. If you change the name to db1.sqlite3 or anything of your choice it will create that file in your root directory every time you run server again.<\/p>\n<p>Here, in this, we have changed the name to \u2018dataflair.sqlite3\u2019.<\/p>\n<p>Now execute these two commands:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py migrate\r\npython manage.py runserver<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair.sqlite3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52911\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair.sqlite3.png\" alt=\"Django Database - dataflair.sqlite3 \" width=\"333\" height=\"151\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair.sqlite3.png 333w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair.sqlite3-150x68.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair.sqlite3-300x136.png 300w\" sizes=\"auto, (max-width: 333px) 100vw, 333px\" \/><\/a><\/p>\n<p>As we can see, a new database file is created and it is such an easy process to create a database with Django framework. Just like this, every database has some attributes which actually become the default dictionaries indexes which you can change\/ create according to the database you are connecting to.<\/p>\n<p>We will be connecting the MySQL database with our project.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-database-access\/\">Learn to perform Python Database Access<\/a><\/strong><\/p>\n<h3>2. MySQL and Django &#8211; Connecting MySQL Database with Django Project<\/h3>\n<p>MySQL is a very powerful database providing you with tons of features and flexibility. We are not going to discuss its functionality now, we will just integrate it with our project.<\/p>\n<p>Here are the steps to integrate Django project with MySQL:<\/p>\n<h5>i. Install Xampp<\/h5>\n<p>Xampp is a free opensource tool which provides you with the Apache server and phpMyAdmin which is the best source for beginner programmers to work with MySQL.<\/p>\n<p>You can download the Xampp according to your system from here:<\/p>\n<p>https:\/\/www.apachefriends.org\/download.html<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52912\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website.png\" alt=\"Django Database - Xampp Website\" width=\"1600\" height=\"752\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website-150x71.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website-300x141.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website-768x361.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website-1024x481.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Website-520x244.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-features\/\">Explore Django Features in detail<\/a><\/strong><\/p>\n<h5>ii. Run Xampp Control Panel<\/h5>\n<p>Now, after installation, you will have to run the Xampp Control Panel and just start 2 services there, Apache and MySQL.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52913\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel.png\" alt=\"Django Database - Xampp Control Panel (Before)\" width=\"835\" height=\"542\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel.png 835w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-150x97.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-300x195.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-768x499.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-520x338.png 520w\" sizes=\"auto, (max-width: 835px) 100vw, 835px\" \/><\/a><\/p>\n<p><strong>Note: <\/strong><br \/>\nStart the Apache Server First and then the MySQL server.<\/p>\n<p>Just click on start action, and after starting it should look like this image.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-after.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52914\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-after.png\" alt=\"Django Database - Xampp Control Panel (after)\" width=\"704\" height=\"221\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-after.png 704w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-after-150x47.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-after-300x94.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Xampp-Control-Panel-after-520x163.png 520w\" sizes=\"auto, (max-width: 704px) 100vw, 704px\" \/><\/a><\/p>\n<p>Now, click on the Admin of the MySQL Service, that should open a webpage(offline) looking like this.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52915\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin.png\" alt=\"Django Database - phpmyadmin screen\" width=\"1600\" height=\"752\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-150x71.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-300x141.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-768x361.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-1024x481.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-520x244.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>This is the main page where we will be observing our database.<\/p>\n<p>The main advantage of Xampp is that it provides you a very interactive environment and when we will be deploying our models, that too will be done in this.<\/p>\n<p>So, we get a very powerful database with an interactive dashboard and one of the world\u2019s best framework with the easiest and most powerful programming language out there.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/sql-rdbms-database\/\">Don&#8217;t forget to check SQL RDBMS Database<\/a><\/strong><\/p>\n<h5>iii. Creating a SQL database:<\/h5>\n<p>On the webpage phpMyAdmin, we will have to create a database for our project. That\u2019s very easy.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Create-new-database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52916\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Create-new-database.png\" alt=\"Django Database - Create new database\" width=\"355\" height=\"247\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Create-new-database.png 355w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Create-new-database-150x104.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Create-new-database-300x209.png 300w\" sizes=\"auto, (max-width: 355px) 100vw, 355px\" \/><\/a><\/p>\n<p>Just Click on the New button as shown here. Then, just fill the desired name of your database and click on create button.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52917\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name.png\" alt=\"Django Database - database name\" width=\"871\" height=\"264\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name.png 871w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name-150x45.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name-300x91.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name-768x233.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/database-name-520x158.png 520w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/a><\/p>\n<p>That will add your database in the list.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52919\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database.png\" alt=\"Django Database - dataflair database\" width=\"298\" height=\"299\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database.png 298w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-160x160.png 160w\" sizes=\"auto, (max-width: 298px) 100vw, 298px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>That\u2019s it, now we don\u2019t need to do anything here. We will be only interacting with python and the <strong>models component of Django<\/strong> will prepare everything for us.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52920\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen.png\" alt=\"Django Database - dataflair database Screen\" width=\"1600\" height=\"752\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen-150x71.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen-300x141.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen-768x361.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen-1024x481.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/dataflair-database-screen-520x244.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>The database we created is empty right now. After the 4th step, there will be lots of tables here holding different information.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-mtv-architecture\/\">Understand the components of Django MTV Architecture<\/a><\/strong><\/p>\n<h5>iv. Modifying settings.py<\/h5>\n<p>In this last step, we will be changing the DATABASE dictionary in our main projects settings.py.<\/p>\n<p>First, install this file via command line:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">pip install mysqlclient<\/pre>\n<p>It will install the Django code for connecting the MySQL Database.<\/p>\n<p>After that exchange this code with the DATABASE dictionary in settings.py.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">DATABASES = {\r\n'default': {\r\n'ENGINE': 'django.db.backends.mysql',\r\n'NAME': 'dataflair',\r\n'USER': 'root',\r\n'PASSWORD': \u201c\u201d,\r\n'HOST': \u201c\u201d,\r\n'PORT': \u201c\u201d,\r\n'OPTIONS': {\r\n'init_command': \"SET sql_mode='STRICT_TRANS_TABLES'\"\r\n}\r\n}\r\n}<\/pre>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52921\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy.png\" alt=\"Django Database - settings.py\" width=\"992\" height=\"410\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy.png 992w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy-150x62.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy-300x124.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy-768x317.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/settingspy-520x215.png 520w\" sizes=\"auto, (max-width: 992px) 100vw, 992px\" \/><\/a><\/p>\n<p>Here the attributes greater in number as MySQL provides us more features from the sqlite3. The Engine here is \u201cdjango.db.backends.mysql\u201d which as the name suggests is a python library for MySQL.<\/p>\n<p>We would recommend though, that you should keep the PASSWORD empty as filling it can produce an error for some users.<\/p>\n<p>Here, the HOST is the host server, but left blank means by default is localhost.<\/p>\n<p>The OPTIONS is a bit interesting attribute, in this, we are actually passing the <strong><a href=\"https:\/\/data-flair.training\/blogs\/sql-tutorial\/\">SQL<\/a><\/strong> as a string through Python which then the SQL server parses itself.<\/p>\n<p>Here we have to write:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">SET sql_mode = \u2018STRICT_TRANS_TABLES\u2019<\/pre>\n<p>It is essentially SQL, being passed on as a string.<\/p>\n<p>For the last steps just run these two commands:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py migrate\r\npython manage.py runserver<\/pre>\n<p>Now, refresh the phpMyAdmin page and you will get some tables created with just doing this much.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-52922\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server.png\" alt=\"phpmyadmin page after after migrating and running server\" width=\"1600\" height=\"752\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server.png 1600w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server-150x71.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server-300x141.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server-768x361.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server-1024x481.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/phpmyadmin-page-after-after-migrating-and-running-server-520x244.png 520w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><\/a><\/p>\n<p>That\u2019s it, you have now all the things ready for your website, all the components, and development setup is finally complete.<\/p>\n<p><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-project-layout\/\">Check out Django Project Layout &amp; Files Structure<\/a><\/strong><\/p>\n<h2>Summary<\/h2>\n<p>We learned how to add the <strong><a href=\"https:\/\/www.mysql.com\/\">MySQL<\/a><\/strong> database to our project and also understood the DATABASE dictionary of the Django Framework.<\/p>\n<p>Also, we learned that Django has middleware and libraries for almost all the important databases out there. And, you won\u2019t need to learn a new database language for that, Django does all that work for you.<\/p>\n<p>Furthermore, if you have come across with any queries, just enter in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous tutorial, we learned the concept of Django Models, now, we will discuss the process to connect database with Django project. Whenever we are creating a web project or any kind of&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":52928,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[19353,19354,19352],"class_list":["post-52888","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-connecting-database-with-django-project","tag-django-and-mysql","tag-django-database"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django Database - How to Connect MySQL Database with Django Project - DataFlair<\/title>\n<meta name=\"description\" content=\"Django database tutorial covers the explanation of DATABASES Dictionary and steps to connect MySQL database with our Django project.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django Database - How to Connect MySQL Database with Django Project - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Django database tutorial covers the explanation of DATABASES Dictionary and steps to connect MySQL database with our Django project.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-database\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-29T09:30:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django Database - How to Connect MySQL Database with Django Project - DataFlair","description":"Django database tutorial covers the explanation of DATABASES Dictionary and steps to connect MySQL database with our Django project.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-database\/","og_locale":"en_US","og_type":"article","og_title":"Django Database - How to Connect MySQL Database with Django Project - DataFlair","og_description":"Django database tutorial covers the explanation of DATABASES Dictionary and steps to connect MySQL database with our Django project.","og_url":"https:\/\/data-flair.training\/blogs\/django-database\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-03-29T09:30:53+00:00","article_modified_time":"2021-06-21T06:49:38+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-database\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-database\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Django Database &#8211; How to Connect MySQL Database with Django Project","datePublished":"2019-03-29T09:30:53+00:00","dateModified":"2021-06-21T06:49:38+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-database\/"},"wordCount":1307,"commentCount":14,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-database\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg","keywords":["Connecting Database with Django Project","Django and MySQL","Django Database"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-database\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-database\/","url":"https:\/\/data-flair.training\/blogs\/django-database\/","name":"Django Database - How to Connect MySQL Database with Django Project - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-database\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-database\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg","datePublished":"2019-03-29T09:30:53+00:00","dateModified":"2021-06-21T06:49:38+00:00","description":"Django database tutorial covers the explanation of DATABASES Dictionary and steps to connect MySQL database with our Django project.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-database\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-database\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Django-Database.jpg","width":1200,"height":628,"caption":"Django Database"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"Django Database &#8211; How to Connect MySQL Database with Django Project"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/52888","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=52888"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/52888\/revisions"}],"predecessor-version":[{"id":53228,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/52888\/revisions\/53228"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/52928"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=52888"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=52888"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=52888"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}