Django and Oscar

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.

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 file. Provision is made for prod, dev and test configurations.
  • has been modified to use the dev configuration.

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

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

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

(aw) $ chmod a+x ./

New Django-Oscar App

The modified Django app template includes:

  • An file.

    from django.urls import path
    from . import views
    urlpatterns = [
        path(route='', view=views.index, name='{{ app_name }}.index'),
  • A modified that subclasses OscarConfig instead of Django’s AppConfig.
  • A static/ directory, where static assets for the app should be placed. The 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:

(aw) $ ./ startapp \
  --template $django/django/django/conf/app_template \

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


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

  $(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


  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

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

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

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

./ 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:

$ addNewAppToInstalledApps

Error: no Django app name specified.

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

  addNewAppToInstalledApps newAppName

* 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.