Welcome to my .py series where I take you through what I learn weekly at She Code Africa Mentorship program Cohort 2-Python track. You can check below for other episodes.
Just as it is very important that you learn the basics of any programming language before hopping on frameworks, it is also important you understand the structure of the framework before building amazing stuff.
In this article I will be taking you through:
Django project structure
Details on the files in the Django project
Django project structure is based on the M-V-T (Model-View-Template) software design pattern. Not again, jargon right, don’t worry I’d simplify that.
For instance you do a google search for ‘beautiful cities’ in the world, in splits seconds, you get the results right. Have you wondered what happened behind the curtains, a glimpse is, the browser sends the keyword ‘beautiful cities’ as a request to the google server which queries the google search index database, fetches the results from the database,puts it in order and displays the result to the user. Not so complex right!
Let's break down M-V-T:
Model: This is what handles or maintains your data in the form of a database (generally relational databases) such as mySQL, Postgresql). Models deals with database operations like creating and updating tables, fetching and updating data.
View: This is what presents the model to the user as a HTTP response( results of your web search ‘beautiful cities’). It takes a web request and returns a web response.
Template: Django mainly functions with backend so to present it better to the user and provide a layout for the website, it makes use of templates.
This diagram below further explains the M-V-T pattern:
So this diagram basically illustrates that view retrieves data from the database via the model and formats it up in an HTTP response object and sends it to the client through the browser.
Now let’s talk about all those strange files that were there when you did django-admin start project. You can check here for an article to start a django project.
Here’s what the directory structure looks like:
Diving into details of what each file is for:
The outer my_site folder: This is your Django project, just look at it like a root directory so feel free to rename it to your taste, it doesn't disrupt the project. Inside this folder you have another folder( inner my_site), db.sqlite3 and manage.py
Db.sqlite3: This is a database to store all your data.
manage.py: This file is a command-line utility for executing django commands from within your project, basically acts as the main file and this is what you will run on the command line.
The inner my_site: When you open this folder, you’d notice it has a whole lot of files. This is your django website application automatically created for you by django, remember it's a framework so less work.
Let’s dive into those files you see in the inner folder:
init.py: This file tells python, hey see this folder(my_site) as a python package so it allows you import functions from other files.
settings.py: You know an advantage of Django is how secure it is. This is like the configuration file for your project. Here you can set up a whole lot of stuff for your web app like authenticate users, security key, installed apps, static files and the rest . You can check here for more details on settings.
urls.py: This file will contain all the urls of your django web app. It defines all the routes in your app, let's say you an \about page, the url should be included in this file.
wsgi.py: WSGI- Web server gateway interface. This file enables web servers to forward requests to web apps or frameworks written in python. Basically it runs your django application on the web.
asgi.py: ASGI- Asynchronous server gateway interface.This works as the wsgi.py but there is an improvement in the sense that neither the client or server needs to wait for each other to communicate, it just happens anytime asynchronously (occuring at the same time)
So far we have looked at the skeleton of a django project in details. Thanks for reading!