Skip to content

mohsen-mahmoodi/django2-project-template

Repository files navigation

Django 2.0+ project template

This is a personalized fork of Django 2.0+ project template with all the basic requirements as used in my projects. The original template met my basic needs but I had to do a lot of modifications before actually starting development. Because all my changes where personal preferences I preferred to create a customized version of the project. Just like the original project, this template make the least mount of assumptions possible while still trying provide a useful setup. Also the base project template has configurations to support heroku deployment and since I use heroku most of the time I will support it in this template too.

Features

How to install

This project template is intended to use Pipenv, also it is not a requirement. I see some people in the community who still prefer using pip + virtualenv. This is why I included both setup procedures.

using pipenv

pip3 install pipenv
mkdir project_name
cd project_name
pipenv --python <version>
pipenv install django
pipenv run django-admin.py startproject \
  --template=https://github.com/mohsen-mahmoodi/django2-project-template/archive/master.zip \
  --name=Procfile \
  --extension=py,md,env,yaml \
  project_name .
pipenv install -r requirements/common.txt
pipenv install -r requirements/dev.txt --dev
rm -rf requirements
cp example.env .env
cp example.logging.yaml logging.yaml

using pip and virtualenv

Install virtualenv and create project directory

pip install virtualenv
mkdir project_dir
cd project_dir
virtualenv -p <python-interpreter> .env
source .env/bin/activate

Install django and create the project

mkdir project_name
cd project_name
pip install django
django-admin.py startproject \
  --template=https://github.com/mohsen-mahmoodi/django2-project-template/archive/master.zip \
  --name=Procfile \
  --extension=py,md,env,yaml \
  project_name .
pip install -r requirements/common.txt
pip install -r requirements/dev.txt
cp example.env .env
cp example.logging.yaml logging.yaml
export $(grep -v '^#' .env | xargs)

Environment variables

These are common between environments. The ENVIRONMENT variable loads the correct settings, possible values are: DEVELOPMENT, STAGING, PRODUCTION.

ENVIRONMENT=DEVELOPMENT
DJANGO_SECRET_KEY=dont-tell-eve
DJANGO_DEBUG=yes
...

These settings(and their default values) are only used on staging and production environments.

DJANGO_SESSION_COOKIE_SECURE=yes
DJANGO_SECURE_BROWSER_XSS_FILTER=yes
DJANGO_SECURE_CONTENT_TYPE_NOSNIFF=yes
DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS=yes
DJANGO_SECURE_HSTS_SECONDS=31536000
DJANGO_SECURE_REDIRECT_EXEMPT=
DJANGO_SECURE_SSL_HOST=
DJANGO_SECURE_SSL_REDIRECT=yes
DJANGO_SECURE_PROXY_SSL_HEADER=HTTP_X_FORWARDED_PROTO,https

Reloading environment variables

After updating or adding new environment variables, make sure to load the variables and reload the project:

Using pipenv run

Nothing required, the variables gets loaded automatically by the pipenv.

Using pipenv shell

You should exit the current pipenv shell by typing exit and reload the session again by running pipenv shell and then run the development server again.

Using pip and virtualenv

just run the command export $(grep -v '^#' .env | xargs) again and restart the development server.

Deployment

It is possible to deploy to Heroku or to your own server.

Heroku

heroku create
heroku addons:add heroku-postgresql:hobby-dev
heroku pg:promote DATABASE_URL
heroku config:set ENVIRONMENT=PRODUCTION
heroku config:set DJANGO_ALLOWED_HOSTS='[app-name].herokuapp.com'
heroku config:set DJANGO_SECRET_KEY=`./manage.py generate_secret_key`
git push heroku master
heroku run python manage.py createsuperuser

License

The MIT License (MIT)

Copyright (c) 2019 Mohsen Mahmoodi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.