Intro to Django

Learning Objectives
Students Will Be Able To: |
---|
Describe the use case of Django |
Contrast MVC with Django's MVT architecture |
Describe the components of a Django project |
Describe Django's routing methodology |
Install Django and psycopg2 |
Build Django's official tutorial app |
Road Map
- What is Django?
- Django's MVT Architecture
- Components of a Django Project
- Django's Routing Methodology
- Installation & Set Up
- Django Tutorial Setup
- Code the Django Official Tutorial App
- Initial Project and Database Setup
- Complete Through Part 4 of the Tutorial
Videos
1. What is Django?
Django is one of the most popular Python-based web frameworks and its popularity continues to grow, thanks to the amazing growth of Python itself.
Django was publicly released in 2005 and got its name from one of its creators, Adrian Holovaty, who named Django after his favorite guitarist: Django Reinhardt.
It's designed for the rapid development of highly-secure web applications.
Compared to the minimalist web framework of Express, Django is a much higher-level framework that provides lots of functionality baked-in, including:
- A powerful Object-Relational-Mapper (ORM) for working with relational databases using Python code instead of SQL.
- A built-in admin app for browsing and manipulating data in the database.
- Built-in user management and authentication.
2. Django's MVT Architecture
Django's architecture is similar, but different than that of MVC:
This chart compares MVC to Django's MVT:
Concern | MVC | Django MVT |
---|---|---|
Database access | Model | Model |
Code mapped to routes | Controller | View |
Rendering of dynamic HTML | View | Template |
When developing with Django, we'll just have to be careful to say:
- view - instead of controller
- template - instead of view
The fundamental difference between MVT and MVC - other than nomenclature - is that:
In Django many of the responsibilities a controller would normally perform are picked up in some way by the framework itself.
You may also realize that the View in MVT appears to be very similar to a controller in MVC, so why not just call views controllers? The reasons for this can be found in the philosophy of the Django development team, who have instituted a slight variation of the MVC model because their framework is unique compared to other MVC frameworks - with a heavy emphasis on convention and ease of use.
- If you'd like to read more on this topic, checkout this clarification provided by the Django team here: Why did they call it MVT if it's basically MVC?.
MVC and MVT are all related to the MV? family of design patterns. These patterns emphasize separating presentation concerns(UI), application processing concerns, and data management concerns. They also have a wide variety of acronyms and are usually only differentiated by small details and marginal philosophical differences between framework developers. Some common MV* patterns are MVC(model, view, controller), MVVM(model, view, view model), MPV(model, view, presenter) and many others. MVT is just another one of the many patterns modeled after MV?.
3. Components of a Django Project
This diagram visually outlines the relationships between the different components of a Django project:

The quirky thing about Django is how it names its high-level components.
What we think of as a web application, Django calls a project.
Furthermore, what we think of as part of an app's functionality (or modules), Django refers to as apps.
A Django project can have many apps, and a Django app can belong to multiple projects. More on this later.
4. Django's Routing Methodology
Because routing is so fundamental to developing web apps, it's worthwhile to know (in advance) an important difference between Express and Django's routing methodology.
Web frameworks such as Express and Ruby on Rails, use both the HTTP verb & URL to define routes used to map requests to controller actions.
Other frameworks, such as Django and ASP.NET Core, use just the URL when defining a route, ignoring the HTTP verb.
That's why the Python modules used to define routes in Django are named urls.py (see diagram above).
5. Installation & Set Up
Prerequisite Installs
Django should have been installed during installfest.
Let's verify the installation by typing django-admin
and pressing enter in terminal. You should receive output similar to the following:
Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runserver
sendtestemail
shell
showmigrations
sqlflush
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
test
testserver
Note that only Django core commands are listed as settings are not
properly configured (error: Requested setting INSTALLED_APPS, but
settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure() before
accessing settings.).
If it's not installed, do a one-time install of the Django framework using the following command:
pip3 install Django
By default, Django uses a lightweight database called SQLite. However, SQLite is not appropriate for production use because it's considered not to be scalable (for example, only one user/request can access the database at a time).
Therefore, from the start we'll be following the better practice of using a more capable database by configuring each of our Django projects to work with PostgreSQL.
To use PostgreSQL, we need to do a one-time install of the psycopg2 Python package:
pip3 install psycopg2-binary
psycopg2
is a popular library that enables Python applications to interface with PostgreSQL.
6. Django Tutorial Setup
Create a Database for the Project
MongoDB automatically creates a database when you use it for the first time.
However, we're not so lucky with SQL databases where we will need to manually create a database that we want to use for a Django project in advance.
Let's create one using the command installed with PostgreSQL:
createdb polls
Alternatively, we could go into psql psql <database_name>
and then create the database as follows:
CREATE DATABASE polls;
Run the \l
command to confirm that polls
is in the list of databases.
Depending upon how PostgreSQL was installed, you might need the database's username/password. Just in case, make note of the username to the right of the database name in the list.
With the database created, type \q
to exit the psql shell.
Move into your ~/code
folder
The last thing to do is to move into your code folder:
cd ~/code
Excited?? Ready to build your first Django app? Let's do this!
7. Code the Django Official Tutorial App
Django, in so many words, is a beast! Its documentation is hundreds of times larger than that of Express.
A great way to get started learning it is by following the official Django tutorial. It's an excellent tutorial and we'll be getting a lot of information straight from the horse's mouth!
Rest assured that we'll be diving deeper into the Django topics touched upon during the tutorial, and more, like learning how to upload images to Amazon S3!
Let's get started by clicking here!
8. Initial Project and Database Setup
To help get you started, we'll cover the following sections together:
- Creating a project (in part 1)
- The development server (in part 1)
- Creating the Polls app (in part 1)
- SKIP Write your first view - you will be completing this section.
- Database setup section of part 2)
9. Complete Through Part 4 of the Tutorial
You'll continue to work on the tutorial, please complete it through Part 4.
❗ Make sure to complete the Write your first view section that we skipped!
Don't complete any parts of the tutorial after Part 4.