Setting up and managing a database for a Django project

Once a working Django project is set up, we will need to attach a database in order to have a place to store the website data and the overall configuration of the Django admin console. For the purpose of this book, we are going to use a SQLite database, which is easy to manage, to connect to the project. It's a file-based database and can be easily managed by Django. For more information on SQLite, refer to its website at http://www.sqlite.org/.

Attaching a database to Django is really easy. You just have to tell Django which database to use and how to connect to it. This has to be done in the Django settings.py file. To connect to and create a SQLite database, the database section of code should look like the following:

Setting up and managing a database for a Django project

Since SQLite is a file-based database, the Name property should be the path of the database file. We are using a constant that contains the project's root path, Project_Root, which has to be defined first with the following two lines of code:

Setting up and managing a database for a Django project

The database is now connected to the Django project. To create the configuration tables in the database, use the Django sync DB command. Visual Studio provides a command, which starts this process from inside the IDE. To access the sync command, right-click on the project node of the Solution Explorer window and select the Django Sync DB entry to start the process:

Setting up and managing a database for a Django project

Once you click on Django Sync DB, the synchronizing starts. The Django Management Console option displays a detailed log of activities as follows:

Setting up and managing a database for a Django project

When the command is first executed, Django creates the actual database and also the authentication entries to be used for the Django administration console. The preferred username and password are asked to be entered. Once the information is provided, the process ends with the database created.

We can also see the result of the process in the Solution Explorer window since we have instructed Django to use SQLite and indicated the location to create the database file. To view this information, enable the View all files option in the solution folder by clicking on the View all files command in the Solution Explorer toolbar.

Setting up and managing a database for a Django project

Find the "View all files" option in the toolbar

Once the view is active, the file db.sqlite3 can be found in the solution folder. It can be included in the solution files by right-clicking on it and selecting the Include in Project command. The SQLite file is then included in the list of files that are managed by Visual Studio.

Setting up and managing a database for a Django project
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.255.36