Django and Oscar
Mike Slinn

General Django Notes

Published 2021-02-12. Last modified 2021-03-29.
Time to read: 2 minutes.

This page is part of the django collection.

General Django-oscar notes are written up separately.

Mozilla Developer Network

The Django documentation is uneven, with some parts being well written, while others are quite poor. In particular, the Django Frobshop tutorial is low quality. The Mozilla Developer Network (MDN) has a much better Django tutorial.

Django Flowchart

Django Flowchart by Hitesh Sarda
Django Flowchart by Hitesh Sarda

Bash Tab Completion

Tab completion for django-admin and manage.py can be enabled as follows.

Shell
$ wget -O ~/django_bash_completion \
  https://raw.githubusercontent.com/django/django/master/extras/django_bash_completion
 $ echo "source django_bash_completion" >> .bashrc
 $ source ~/django_bash_completion

Tab completion for django-admin and manage.py in derivative projects, such as django-oscar, is automatic.

Initial Project Creator Script

#!/bin/bash

function help {
  echo "$(dirname $0) - Create a new django project with a matching Python virtual environment.

Installs the latest stable release of Django in the new Python venv,
which has the same name as the new Django webapp.
This means that all of the pip modules in the new Python venv exactly
match the requirements of the new Django webapp.

A Django app is also created with the same name as the Django webapp itself.

Usage: $(dirname $0) newDjangoWebAppName
... then navigate to http://127.0.0.1:8000/ and view the standard Django initial welcome page.
"
  exit 1
}

if [ -z "$1" ]; then help; fi


# Create a virtual environment in subdirectory "venv" for this Django project
python -m venv "$1"
source "$1" /bin/activate  # Activate it

pip install Django  # Install the latest stable release
# pip install Django==2.1  # Install a specific version

django-admin startproject "$1"  # create the Django project
python ./manage.py migrate  # run migrations
python ./manage.py createsuperuser  # create a super user

if [[ `service postgresql status` == **down ]]; then
  sudo service postgresql start
fi
./python manage.py runserver --noreload $* 0.0.0.0:8000  # start the development server

Settings for Production, Development, Testing, and Continuous Integration

Django has a simple and effective mechanism for establishing settings for various environments. Common environments include production (prod), development (dev), testing (test), and continuous integration (ci). The Frobshop tutorial just sets up one settings environment. The process necessary to refine that collection of settings into multiple environments is well documented and easy to work through.

Site Map

Sitemaps support is built into Django. However, Matthias Kestenholz wrote the django-sitemaps PIP module which also features alternates (alternates allow communicating the pages that contain the same content in different languages).

Tutorials:

Django-robots generates the robots.txt exclusion file for Django. This PIP modules complements the Django sitemaps.

Variables and Filters

The syntax for defining and using Django variables is almost identical to the syntax for using Jekyll / Liquid variables.

Similarly, Django filter syntax is identical to Jekyll / Liquid filter syntax.

BASE_DIR Setting

It is common to set up a Django project for dev, prod and perhaps test settings. These settings usually inherit from base settings, which are defined in a file called your_django_webapp/your_app/settings/base.py. Within base.py, BASE_DIR might be defined like this:

base.py fragment
BASE_DIR = Path(__file__).resolve().parent.parent.parent

The shell subcommand to manage.py accepts a line to be interpreted by using the -c option. Here is a one-line solution that displays BASE_DIR for a Django app called frobshop:

Shell
$ ./manage.py shell -c \
'from frobshop.settings import base; print(f"BASE_DIR={base.BASE_DIR}")'
BASE_DIR=/var/work/django/frobshop 

This StackOverflow question has several excellent answers.



* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.