Skip to content

Intro to Django

Laura Beaufort edited this page Dec 10, 2018 · 7 revisions

What is Django?

Django is a web framework built in Python. It debuted in 2003 to help build a newspaper web app, and has since been the base for many high scaling and performant web sites, apps, and APIs since then. New version information can be found on the Django website.

Core Framework

Django is a MVC (Model View Controller) web framework that has many built in components.

Highlights include:

  • Local development web server
  • Form serialization and validation
  • Template system
  • Caching
  • Middleware
  • Internationalization
  • Serialization (JSON model instances)
  • Test framework
  • Built in admin system
  • A robust and healthy ecosystem of many reusable apps, including Wagtail CMS!

Basic Prerequisites

  • Python 3.4.x
  • Virtualenv
  • Pip

Check for Python install:

python -V

Make sure it returns Python 3.4 or higher.

Set up your virtual environment, and activate it.

There are multiple ways to accomplish this, so please feel free to use whatever works for you. If you're unsure, here is one example that you can follow to create one and then activate it:

pyvenv .venv
. .venv/bin/activate

Pip install Django:

pip install django

MVC Overview

While Django is considered a MVC app, it can more accurately be described as a "MTV" framework: Model Template View.

Models

The model controls the data. This is how you describe your database layout in Python. Example:

class PressReleasePage(ContentPage):
    date = models.DateField(default=datetime.date.today)
    formatted_title = models.CharField(max_length=255, null=True, blank=True, default='')
    category = models.CharField(max_length=255)
    read_next = models.ForeignKey('PressReleasePage', blank=True, null=True)

Views

The view is the interface to view and modify the data. Generally the view retrieves data according to parameters, may modify the data in some way (or not), and then loads a template and renders the data. Example:

def get_digests(year=False):
    digests = DigestPage.objects.live()
    if year != '':
      digests = digests.filter(date__year=year)
    return digests

Templates

Templates are used to generate the HTML dynamically. The Django templating language is the default, but other template engines can be configured as well. Example:

{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-{{ self.get_verbose_name | slugify }}{% endblock %}

{% block content %}

{% include 'partials/breadcrumbs.html' with page=self links=self.get_ancestors style='secondary' %}

<article class="main">
  <div class="container">
    <header class="heading--main">
      <h1>{{ self.title }}</h1>
    </header>
    <div class="main__content">
      {{ self.body }}
    </div>
    {% if self.sidebar %}
    <div class="sidebar-container">
      <aside class="sidebar sidebar--secondary">
        {% for block in self.sidebar %}
          {% if block.block_type == 'heading' %}
            <h4 class="sidebar__title">{{ block }}</h4>
          {% else %}
            <div class="sidebar__content">
              {{ block }}
            </div>
          {% endif %}
        {% endfor %}
      </aside>
    </div>
    {% endif %}
  </div>
</article>

{% include 'partials/disclaimer.html' %}

{% endblock %}