What is Django? A Quick Django Tutorial Part 1

What is Django?

Written in Python, Django is the self-proclaimed web framework for perfectionists with deadlines – and I have to agree. Django provides so much power out of the box and is built on Python – which has its own repository of libraries, PyPI – that you can lean on. It’s easy to understand why Django is the top Python web framework today and is among the top six of all programming frameworks.

The Django Framework

Django is a “batteries included” framework. There’s a built-in tool to help you with anything you might want to accomplish. Need user authentication? Django has you covered. Need to validate data from user input? You got it. Need to store data in the database after cleaning it? Yep, can do. It even has built-in features that keep you from unintentionally leaving your project open to security holes – like cross-site forgery protection built into every request. Everything is there and ready for you to work with.

Despite all that power and popularity – or possibly because of it – Django has a high learning curve. Thankfully, the documentation about Django’s capabilities is excellent, and this tutorial tutorial will get you over that curve and into Django.

Django

Before we get into the tutorial, I’d like to go over some of the building blocks that make up a Django application.

Django project

Django was designed in a way that encourages modularity, and tying all the modules together is the ultimate goal of your Django project. You can think of each individual website you’d like to create as a unique Django project made up of multiple Django apps.

Django app

A Django project is comprised of Django apps. Ideally, Django apps encapsulate a single feature set: the Unix philosophy of “do one thing and do it well” strongly applies here. Django apps are loosely coupled from the rest of the project and are installed into the Django project through the Django settings file. Generally, you should be able to plug a Django app into any Django project, further encouraging code reuse.

Django Models

Django models are classes that define what tables and columns get created in the database. When you access the database through Django’s ORM (Object Relational Mapping), you get instances of your Model class and can access the data through the fields you defined on that Model. Interacting with the model instance will cause reads and writes to and from the database. Learn more about Models here.

Django Views

Views are situated between what the user sees when they hit your Django project from their browser, and the data that you have in your database. A View takes a web request and returns a web response back to the browser. The View is where the logic of “what do we want to return to this particular request?” lives. By default, Django Views return HttpRequests but they can return JSON, HTML, XML, attachments, or whatever you want them to – as long as it’s contained in a Response object.

Historically, Django Views were just functions and you were mostly on your own when writing them. Now, Django has an entire suite of Generic Class-Based Views that you can configure and use right out of the box. They cover the majority of views you’ll need in any given application, and since they’re Python classes, they’re also inheritable and extensible. Familiarize yourself with them here.

Django Templates

The Django template is a mix of HTML and Django’s own templating language which consists of tags, variables, and filters. The template is Django’s presentation layer: this is where things get pretty. The template can do everything an HTML file can do, with the addition of being able to do some light logic processing and gaining access to any data that’s passed to the template from the View. When Django evaluates the template, variable tags are replaced with actual data, and any logical template tags are processed before Django outputs the final rendered template to the request. You can read more about templates here.

Django Tutorial

For this tutorial, I will be using Django 2.1.7 and Python 3.6.7. You can access the code from this tutorial on Kite’s github repository.

Let’s set the stage for a fun Django project!

Back to Top