Project templates Alternatives and tools

By Emmanuelle Delescolle

Who am I?

  • An average developper
  • Who loves working with Django
  • A woman who codes
  • Someone prone to burnouts

Special thanks to

Naomi Cerder

The Django community

Daniele Procida & the team of volunteers

What does a Django project look like?

???

.
├── core
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
├── other_app
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── project
    ├── __init__.py
    ├── __pycache__
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Who has seen this talk?

Mark Lavin: Anatomy of a Django Project

 Django project is not a thing

A better way?

project
├── environ.py.dist
├── manage.py
├── project
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   └── wsgi.py
├── requirements.txt
├── settings.py
└── urls.py

A better way?

INSTALLED_APPS = (
    'project',

    'django.contrib.admin',
    ...
)
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': (
                'django.template.loaders.app_directories.Loader',
            ),
        },
    },
]

How?

$ hg clone https://bitbucket.org/levit_scs/django_project_template
$ django-admin startproject --template 
        django_project_template/project_app/ project

How?

$ django-admin startproject --template 
        django_project_template/drf_app/ project
$ django-admin startproject --template 
        django_project_template/reusable_app/ project

What does a project template look like?

django_project_template/project_app
├── environ.py.dist
├── manage.py
├── project_name
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   └── wsgi.py
├── requirements.txt
├── settings.py
└── urls.py

What does a project template look like?

"""
Django settings for {{ project_name }} project.

For more information on this file, see
https://docs.djangoproject.com/en/{{ docs_version }}/
  topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/{{ docs_version }}/ref/
  settings/
"""

Problem?

Only python source files are templatable by default

Solution:

cookiecutter by audreyr and pydanny

Features of cookiecutter

  • Every file type is templatable (using Jinja2)
  • You can add hooks to automate project creation further more
$ cookiecutter django_project_template/cc_project_app/
project_name (default is "Project name")? My great project
repo_name (default is "my_great_project")? 
author (default is "Your Name")? Emma

Starting a new project using cookiecutter

my_great_project/
├── environ.py.dist
├── LICENSE.txt
├── manage.py
├── my_great_project
│   ├── __init__.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   └── wsgi.py
├── README.md
├── requirements.txt
├── settings.py
└── urls.py


$ cat README.md

# Emma's My great project

Project description goes here

What does a cookie- cutter template look like?

django_project_template/cc_project_app
├── cookiecutter.json
└── {{cookiecutter.repo_name}}
    ├── {{cookiecutter.repo_name}}
    │   ├── __init__.py
    │   ├── migrations
    │   │   └── __init__.py
    │   ├── models.py
    │   └── wsgi.py
    ├── environ.py.dist
    ├── LICENSE.txt
    ├── manage.py
    ├── README.md
    ├── requirements.txt
    ├── settings.py
    └── urls.py

What does a cookie- cutter template look like?

/* django_project_template/cc_project_app/cookiecutter.json */
{
  "project_name": "Project name",
  "repo_name": 
    "{{ cookiecutter.project_name|replace(' ', '_')|lower() }}",
  "author": "Your Name"
}

Hooks?

#!/bin/bash
USER="{{ cookiecutter.username }}"
EMAIL="{{ cookiecutter.email }}"
virtualenv -p {{ cookiecutter.python }} venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
cp environ.py.dist environ.py
python manage.py migrate --noinput
echo "Creating superuper {{ cookiecutter.username }}"
python manage.py createsuperuser --username {{ cookiecutter.username }} --email {{ cookiecutter.email }}
wget https://github.com/twbs/bootstrap-sass/archive/v{{ cookiecutter.bootstrap_version }}.tar.gz
tar -xzf v{{ cookiecutter.bootstrap_version }}.tar.gz
mkdir -p {{ cookiecutter.repo_name}}/static/js
mv bootstrap-sass-{{ cookiecutter.bootstrap_version }}/assets/javascripts/* {{ cookiecutter.repo_name}}/static/js/
mv bootstrap-sass-{{ cookiecutter.bootstrap_version }}/assets/fonts {{ cookiecutter.repo_name}}/static/
mv bootstrap-sass-{{ cookiecutter.bootstrap_version }}/assets/stylesheets/* sass/css/
rm v{{ cookiecutter.bootstrap_version }}.tar.gz
rm -rf bootstrap-sass-{{ cookiecutter.bootstrap_version }}
wget -O {{ cookiecutter.repo_name }}/static/js/jquery.min.js http://code.jquery.com/jquery-{{ cookiecutter.jquery_version}}.min.js

More?

Workshop friday morning

Django Polla sprint

Questions

Django Project Templates and more

By Emma

Django Project Templates and more

DjangoCon Europe 2015 talk

  • 2,576