Django

Course notes

Project Directory Structure

  1. To start a Django project, enter the command below in terminal:

    # django-admin startproject <project-name>
    django-admin startproject project_name
    
    Copy to clipboard
  2. To create an app, cd to the project root (project_name/ in this case) and run:

    # python manage.py startapp <app-name>
    python manage.py startapp app1
    
    Copy to clipboard

This creates the directory stucture below:

project_name/
├── project_name/    # website App: project-level configurations
│   ├── __init__.py
│   ├── settings.py  # Link Apps: INSTALLED_APPS
│   ├── urls.py      # Link apps' routes to main site 
│   └── wsgi.py
│ 
├── app1/
│   ├── migrations/
│   ├── templates/
│   │   └── app1/
│   │       ├── base.html
│   │       └── index.html
│   ├── static/
│   │       └── style.css
│   ├── __init__.py
│   ├── admin.py     # Customize administrator panel
│   ├── apps.py
│   ├── tests.py
│   ├── models.py    # app database definition
│   ├── urls.py      # app route
│   └── views.py     # app functions
│
├── manage.py
└── db.sqlite3
Copy to clipboard

Common Commands

  • Start new Django project

    django-admin startproject <project-name>
    
    Copy to clipboard
  • Add new app

    python manage.py startapp <app-name>
    
    Copy to clipboard
  • Start server

    python manage.py runserver
    
    Copy to clipboard
  • Update database (after modifying <app-name>/models.py

    python manage.py makemigrations
    python manage.py migrate
    
    Copy to clipboard
  • Django shell

    python manage.py shell
    python manage.py shell < script.py
    
    Copy to clipboard
  • Create super user account

    python manage.py createsuperuser
    
    Copy to clipboard

settings.py

Every time a new app is created, remember to attach this new app to the project. To do this, find INSTALLED_APPS in project_name/settings.py and add the app to the list:

urls.py

Every app has one urls.py and project_name/ has one urls.py. project_name/urls.py defines the url relations of every app to the site. <app-name>/urls.py defines the relations between individual functions in <app-name>/views.py and url paths within an app.

For example, project_name/urls.py might look like the code below, where flights, users and admin (default Django app) are apps in the project:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('flights/', include('flights.urls')),
    path('users/', include('users.urls')),
    path('admin/', admin.site.urls),
]
Copy to clipboard

flights/urls.py may look like the code below:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:flight_id>', views.flight, name='flight'),
    path('<int:flight_id>/book', views.book, name='book')
]
Copy to clipboard

The path functions essentially links the functions in views.py and url paths together in an app.

As an example, (after running the server locally) if you visit http://127.0.0.1:8000/flights/1/book, you will be on a page handled by the function book() defined in flights/views.py. This relationship is defined in path('<int:flight_id>/book', views.book, name='book'). The path right after the base url (http://127.0.0.1:8000/) is flights/ because it was defined in path('flights/', include('flights.urls')), in project_name/urls.py.