Mike Slinn
Mike Slinn

Django-Oscar Project and App Templates

Published 2021-03-07. Last modified 2021-06-09.
Time to read: 2 minutes.

This page is part of the django collection, categorized under Django, Django-Oscar, Visual Studio Code.

When most people discuss Django templates they are referring to views that have special markup that enables variable substitution and simple control structures. This article discusses a different type of Django template: Files and directories that are used as skeletons for creating new Django web applications and apps.

Getting Started

I forked the Django project and cloned it into a directory called $django/django. I then created a branch called django-oscar, and modified django/conf/app_template and django/conf/project_template for a typical django-oscar project.

New Django-Oscar Project

The modified Django app template includes:

  • A settings module instead of just a settings.py file. Provision is made for prod, dev and test configurations.
  • manage.py has been modified to use the dev configuration.

Next time I need to start a new django-oscar project, I will type:

Shell
(aw) $ mkdir $django/djangoProject && cd $django/djangoProject

(aw) $ django-admin startproject \
  --template $django/django/django/conf/project_template \
  myProject

(aw) $ chmod a+x ./manage.py
😁

New Django-Oscar App

The modified Django app template includes:

  • An urls.py file.

    from django.urls import path
    
    from . import views
    
    urlpatterns = [
        path(route='', view=views.index, name='{{ app_name }}.index'),
    ]
    
  • A modified apps.py-tpl that subclasses OscarConfig instead of Django’s AppConfig.
  • A static/ directory, where static assets for the app should be placed. The manage.py collectstatic subcommand collects all of these static assets and copies them to STATIC_ROOT for deployment.
  • A templates/ directory, where templates for the app should be placed.
  • A templatetags/ directory, where template tags for the app should be placed.

Next time I need to start a new django-oscar app, I could type:

Shell
(aw) $ ./manage.py startapp \
  --template $django/django/django/conf/app_template \
  myApp

However, I also want to insert the new app into the array of INSTALLED_APPS, so I wrote a little bash script which calls manage.py as shown above, and then inserts the app name into settings. I stored the bash script as django/extras/addNewAppToInstalledApps:

#!/bin/bash

function help {
  printf "$1
$(basename $0) - Add a new Django app name to the top of 'INSTALLED_APPS' in settings.

Usage:
  $(basename $0) newAppName

The first letter of newAppName will be appropriately capitalized where necessary.

This script requires that Django be checked out into $django/django
and the Django git project branch be set to django-oscar.

NOTE: If your Django webapp uses corsheaders, ensure that it is the first app listed after this script finishes.
"
  exit 1
}

if [ -z "$1" ]; then help "\nError: no Django app name specified.\n"; fi

NEW_APP_NAME="$1"
shift

REGEX='^INSTALLED_APPS ='
SETTINGS_FILE="$(
  grep -lrI "$REGEX" . \
    --include=\*.py \
    --exclude-dir={.git,.idea,.vscode,bin,cache,node_modules} | \
  tail -n 1
)"

if [ -z "$SETTINGS_FILE" ]; then
  echo "Error: no Python file with contents matching '$REGEX' was found in $(pwd)"
  exit 2
fi

if [ -z "$django" ]; then
  echo "Error: Please define an environment variable called 'django', pointing to the Django git project."
  exit 2
fi

if [ ! -d "$django/django" ]; then
  echo "Error: The '$django/django' directory does not exist.
Please clone the Django git project into $django and checkout the 'django-oscar' branch."
  exit 2
fi

APP_TEMPLATE="$django/django/django/conf/app_template"
if [ ! -d "$APP_TEMPLATE" ]; then
  echo "Error: The '$APP_TEMPLATE' directory does not exist. Please checkout the 'django-oscar' branch in '$django/django'."
  exit 2
fi


./manage.py startapp --template "$APP_TEMPLATE" "$NEW_APP_NAME"

sed -i "/$REGEX.*/a \ \ \ \ '${NEW_APP_NAME,,}.apps.${NEW_APP_NAME^}Config'," "$SETTINGS_FILE"

Here is the help message:

Shell
$ addNewAppToInstalledApps

Error: no Django app name specified.

addNewAppToInstalledApps - Add a new Django app name to the top of 'INSTALLED_APPS' in settings.

Usage:
  addNewAppToInstalledApps newAppName
😁