Skip to content

Django Development Tutorial (old)

Oscar Chan edited this page Dec 21, 2022 · 1 revision

Overview

Here, we'll build an entire Django application, from scratch. This is based largely on the Django tutorial, which covers everything here in more detail.

I've assumed you've already setup your system for development, and have some understanding of Python.

So what are we building? An events calendar:

  • Display events in a list, agenda-style
  • Add / remove events, through the admin interface

This will involve the following topics:

  • Creating an application
  • Routing
  • Views
  • Templates
  • Models
  • Admin interface

Creating an application

We'll first create a new project folder, with a venv virtual environment:

source .venv/bin/activate
mkdir hknwebappname
cd hknwebappname

With Django installed, we can now create the Django project:

django-admin startproject hknwebappname ./

If this didn't work, see the django-admin troubleshooting page page.

startproject creates the basic Django project structure, as described in layout:

hknevents/
    manage.py
    hknevents/
        __init__.py
        settings.py
        urls.py
        wsgi.py

The dev server

To check the project works, cd into the outer hknevents and run:

make dev

You should see the following output:

Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

October 23, 2018 - 15:50:53
Django version 2.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Note: You may ignore the 'unapplied migrations' warning for now.

This is the Django development server, a lightweight Web server written purely in Python. This is not intended for production (publicly visible) use, but for development it makes testing much faster.

Here, 'web server' means a program that handles network communications. Our Django project is not technically a web server, but a web application; it depends on a web server to feed it requests from the network, and it will handle the rest of the logic. As for why, see basics.

Now that the server’s running, visit http://127.0.0.1:8000/ with your Web browser. You’ll see a “Congratulations!” page, with a rocket taking off. It worked!

If you would like to make an admin account locally, simply run

make createsuperuser

which is short for (or a Makefile abstraction of)

python manage.py createsuperuser

You will be prompted to enter some login info, and then you can access the admin interface at localhost:[port]/admin.

Note: the default port is 8000. Applications that must share network access on a single computer use different ports, like apartment numbers at a single address, so network traffic can be addressed to ports.

If you want to change the server's port, pass it as a command-line argument:

python manage.py runserver 8888

If you want to change the IP, pass it along with the port:

python manage.py runserver 0.0.0.0:8888

This is helpful if you are running this in Vagrant (where you must use 0.0.0.0) or if your computer has multiple IPs (static / dynamic IPs, WiFi / Ethernet, IPv4 / IPv6, etc.).

The server will automatically reload when you change Python files. If you change other files, like HTML templates, you may have to restart the server manually (Ctrl-C to kill).

Creating the app

There is a lot to cover for creating the app

The main overview is that you need to create "models" as Tables in the backend in order to use with the "views" that map to the HTML inside the templates folder

URLs to the app is handled by the "urls.py"

Advanced topics

I need to use the settings in secrets.py.

Get BlackBox set up with this link: https://github.com/StackExchange/blackbox. You should run blackbox_edit_start secrets.py.pgp to decrypt it to secrets.py. Now your code can access the variables inside. If you edit this file, make sure to encrypt it again and push your changes as secrets.py.pgp. Note that this file is ignored by git, so you shouldn't be able push it in cleartext. Don't.