Love Python AND Web Development? Try this!

CS467 – Blog #3

Image by Michael Schwarzenberger from Pixabay

You don’t live as long as I have without a healthy fear of snakes, Bobby.
Creed Bratton

Meet Django

Django is a full-stack web framework that makes it quick and easy to create highly extensible web applications. Django is a framework written in Python that allows developers to make fast, scalable, and secure websites. Django comes pre-loaded with modern security features that help prevent SQL injection, clickjacking, cross-site scripting and others. Since Django is an open-source project, security patches are continuously being released and vulnerabilities are often found quickly.

Django is one of the most popular Pythonic web frameworks around. (Flask is the other most popular Pythonic web framework. Perhaps another blog post will discuss the differences and when to choose one over the other…)

A lot of popular websites use the Django Framework or part of the Django framework.

Popular Sites that use Django

Instagram (ever heard of it?)

Spotify

YouTube

The Washington Post, The Guardian, and The New York Post

BitBucket (like GitHub)

DropBox

Mozilla

Pinterest

Source: djangostars.com

Structure of a Django Project (Model-View-Template)

Django utilizes a Model-View-Template structure which standardizes the way that a Django project is structured. This helps keep project files nice and organized as well as makes the logic of how Django works compartmentalized. The MVT structure is based on the Model-View-Controller (MVC) structure found in C#/.NET web applications as well as AngularJS and ReactJS libraries.

The Model (Think Database)

The Model is a Python class that is used to set up the structure of a single table in a database. With the models.py file within each Django application directory, you can structure the table any way you want. But instead of creating tables in SQL, you use Python and have Django’s Object-Relational Mapping layer do the heavy lifting.

The View (Think Backend)

Views contain the “business logic” which handles how data is created, stored, and changed. In other words, it handles the backend logic that takes data from the database and sends it to the frontend for the user to view/mess around with.

For example, the views.py file for each Django application directory handles the CRUD. That is, Create, Read, Update, and Delete operations on the data. The logic for handling the CRUD is housed within the views.py files.

Also, the views.py files handle rendering the templates and passing data into the templates for the user to view.

The Template (Think Frontend)

Templates are HTML files that contain the code that the user sees and interacts with. All of the forms, images, text are housed within the template files.

Django utilizes the Jinja2 templating engine which allows data from the views.py file to be passed into the frontend HTML pages. For instance, if you had a list of Employee names stored in a database and wanted to display them all in a table format, you would have an HTML template that contains a for loop requesting data from the views.py file which in turn requests the data from the models.py which in turn gets that data from the data base.

Like I mentioned, the Jinja2 templating engine allows for Pythonic for loops and conditionals to be inserted into HTML files bridging the gap between the front and back end.

Image of Jamie Foxx from the Motion Picture Django Unchained from Columbia Pictures

Tutorials

Corey Schafer makes a wonderful Django tutorial where he will guide you through making a full-fledge Blog website!

Here is Django’s own tutorial and documentation which is not as fun to go through as a video tutorial, but provides great insight on how Django works and where to go for help if something isn’t working quite right.

Give the tutorials a shot to learn how to use Django! If you run into any issues, leave a comment below.