Creating the Config App with Django
Explore how to create and configure the Django Config app within your backend service. Understand the purpose of essential files such as settings.py and urls.py, perform migrations, and prepare your project for Dockerization and database connection using MySQL.
We'll cover the following...
The Config app content
Previously, we created our project inside the backend directory by running the django-admin, startproject, and config commands. If we navigate to this backend directory, we'll see a folder named config, which the django-admin automatically created for us. Inside this folder are the manage.py and db.sqlite3 files and another folder with the same name as the outer folder. This inner config folder is a Python package, which is our Config app.
The illustration below shows the content of our project folder, which contains our Config app.
Let’s look at what all these files do:
The outer
config/folder: This is the project folder created by Django. Django does not alter the name of this folder, so we can rename it to whatever we want. Later, we'll rename it tobackendservice1.The inner
config/folder: This is the inner project folder and our Config app. It is a Python package that we do not rename.The
__init__.pyfile: This is a Python file inside the innerconfig/that tells the Python interpreter that our Config app is an actual Python package.The
asgi.pyfile: This helps web servers compatible with the Asynchronous Server Gateway Interface serve our project.The
settings.pyfile: This will contain all of our Django project settings.The
urls.pyfile: This will contain all of our project’s URLs/endpoints.The
wsgi.pyfile: This Python file helps web servers compatible with the Web Server Gateway Interface serve our project. It helps with deployment during production.The
manage.pyfile: This outerconfig/file helps us execute several Django commands in the command line for our project.The
db.sqlite3file: This is an SQLite database file inside the outerconfig/folder. We won't use this file since we'll use the MySQL database.
Although there are a lot of files, we'll only work with the settings.py and urls.py ones.
Exploring the settings.py and urls.py files
The settings.py and urls.py are two important files in our Config app. The behavior of our project is dependent on the settings.py file. So we need to be familiar with it. As we develop our project and add components, we'll always return to the settings.py file to make adjustments. Similarly, as we define and add routes, we must update the urls.py file.
Now, let's take some time to explore these two files, focusing on the highlighted lines. Also, click the “Run” button to apply migrations before the application dockerization.
NOTE: At any point in this course, if you get an error or a warning on the widget’s output the first time you click the “Run” button, click the “Run” button again.