This blog post is intended mainly for newbies to moderately experienced developers. Django, needless to say, is a web application framework for
Python programmers, that helps in shipping things quickly to production from the concept stage. Most of the heavy lifting is done by the framework.
Most commands and steps in this article will fit into
*nix based systems like Mac, Ubuntu. And we will be using
python3. Without further ado, let’s jump right into the topic.
Django is based on a design pattern where the different components/tasks are abstracted into three core ideas, the model, view, and template.
- Model is your data access layer, which is responsible for doing CRUD operations on your data
- View is the business logic orchestration layer where your most of logic resides
- Template is like the frontend of your web application, where you define how users see your application
This pattern is a little counter-intuitive if you are coming from a Rails background where most of the things are written around MVC(Model-View-Controller) pattern, where View represents the frontend and business logic is handled by Controller. But, you have to deal with this confusion if you are into both Django and Rails 🙂.
How a typical HTTP request flows in the app?
Now that you know what MVT pattern is and how Django leverages that abstraction. Let’s understand how a typical HTTP request flows in a Django app.
- Requests hit the Django webserver
- Django decides which view to handover the request to
- View does some validations, etc.
- Model pulls out data or does relevant operations on the database
- Response is composed
- If it is a page that is to be rendered, the corresponding template is utilized within the response context and the page is rendered
Making sure Django is installed and starting an app
You can check if Django is installed on your machine with the below command. If it is installed you will see a version otherwise an error message saying no module is installed.
python3 -m django --version
Django projects are organized in-terms of apps. You plug multiple apps into your project.
Let’s create a Django project, the below command, where
distributedstack being the project that is created.
django-admin startproject distributedstack
The above command creates a bunch of files and a directory with the project name. This is where your core project stuff like URLs, WSGI server, settings reside. You might have noticed
manage.py, this is a utility for doing various things around your Django project like creating migrations, running them, starting the server, etc.
You can create apps with the below command, where
posts is the app that is being created.
python3 manage.py startapp posts
This command will create the app directory, which contains different files that fit into the MVT pattern we discussed above.
Some best practices
Now that you understand to some extent and can dig further with this knowledge. Let’s also look at some of the best practices that will help you in avoiding pitfalls.
- Keep apps as de-coupled as possible, treat them as independent pieces of software which can be used elsewhere
- Create singleton objects for costly resources like database connections etc.