-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of template that includes the .env file.
- Loading branch information
0 parents
commit 066be19
Showing
19 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# This .env file is to only be used for development mode, it's not suitable for use in production. | ||
export SECRET_KEY='MY DEVELOPMENT-ONLY SECRET KEY THAT SHOULD ONLY BE USED ON MY LOCAL MACHINE AND NOT IN PRODUCTION.' | ||
export DEBUG=true | ||
export TEMPLATE_DEBUG=true | ||
export DATABASE_URL='sqlite' | ||
export PYTHONUNBUFFERED=true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Text Editors | ||
*.swp | ||
|
||
# File Systems | ||
*.DS_Store | ||
|
||
# SQLite Database | ||
*.sqlite3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
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 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. | ||
|
||
For more information, please refer to <http://unlicense.org> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
web: gunicorn {{ project_name }}.wsgi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Heroku Django 1.7.x Template | ||
|
||
>**WARNING: UNLESS YOU WANT TO MANUALLY MODIFY THE TEMPLATE BEFORE PROCESSING IT, DO NOT CLONE THIS REPOSITORY AS IT CONTAINS A ```.env``` FILE WITH SETTINGS ONLY SUITABLE FOR LOCAL DEVELOPMENT. NEVER PUSH THIS REPOSITORY TO A PRODUCTION SERVER.** | ||
To use this Django Project Template, you should run this in your terminal (obviously replace name_of_your_project to the name that you want your project to have): | ||
``` | ||
django-admin.py startproject --template=https://github.com/jeremypearson/heroku-django-1.7.x-template/archive/master.zip --name=Procfile name_of_your_project | ||
``` | ||
Inspired by: https://github.com/heroku/heroku-django-template and https://github.com/heroku/python-getting-started. | ||
This is a basic Django 1.7.x Project Template, which contains only one Django Application. The project just displays the current date and time in UTC every time that the user loads the webpage. | ||
The great thing about this Project Template, is that it's set up in a way that encourages the developer who uses it correctly to keep all of their secrets out of the source-code. It also encourages them to set DEBUG and DEBUG_TEMPLATES outside of the source-code. This is done through the use of environment variables. | ||
|
||
Note: This Django Project Template has only been tested for Django 1.7.9. If you use another version Django 1.7.x and run into problems, please create 'issue' on the Github repository: https://github.com/jeremypearson/heroku-django-1.7.x-template | ||
|
||
## Running the Django project locally | ||
Note: These instructions are specific to Mac OS X, if you have a different platform, they may need to be slightly adjusted. | ||
|
||
### Pre-requisites | ||
You have to already have installed and correctly configured all of the following: | ||
* Python (https://www.python.org/downloads/) | ||
* Pip (https://pip.pypa.io/en/latest/installing.html) | ||
* Virtualenv (sudo pip install virtualenv) | ||
* Virtualenvwrapper (sudo pip install virtualenvwrapper) | ||
* Git (https://git-scm.com/downloads) | ||
* Heroku Toolbelt (https://toolbelt.heroku.com) | ||
|
||
If you want the Django Project to use a PostgreSQL database instead of an SQLite database while it's running on your local machine, then you'll also have to have already installed PostgreSQL (http://www.postgresql.org/download/). | ||
|
||
Note: While Heroku recommends that you use the same database in development as you do in production, you can choose not to. So you could just make the Django Project automatically use an SQLite database when it's running on your local machine, and automatically use a PostgreSQL database when it's running on Heroku. | ||
|
||
### Instructions | ||
#### Running the Django Project on your local machine, initially using the SQLite database backend. | ||
1. Change into the directory that will hold the directory of the Django Project. For example: ```cd ~/dev/``` | ||
1. Create a virtualenv for it: ```mkvirtualenv name_of_your_project``` | ||
1. Install Django 1.7.9: ```pip install django==1.7.9``` | ||
1. Create a project using the project template: ```django-admin.py startproject --template=https://github.com/jeremypearson/heroku-django-1.7.x-template/archive/master.zip --name=Procfile name_of_your_project``` | ||
1. Change into the directory of the project you just created, for example: ```cd ~/dev/name_of_your_project``` | ||
1. Set this directory as the project directory of the virtualenv: ```setvirtualenvproject``` | ||
1. Install the project's dependencies: ```pip install -r requirements.txt``` | ||
1. Optionally change the following environment variables, within the ```.env``` file to the values you want for your local machine: ```SECRET_KEY```, ```DEBUG```, ```TEMPLATE_DEBUG```, ```PYTHONUNBUFFERED```. See section [The .env file](#the-env-file) for more information. | ||
1. Source the ```.env``` file, so that the environment variables will be set for when we run ```manage.py```: ```source .env``` | ||
1. Make migrations: ```python manage.py makemigrations``` | ||
1. Apply migrations: ```python manage.py migrate``` | ||
1. Run the Django Project using ```foreman``` (```foreman``` is a part of the Heroku Toolbelt): ```foreman start``` | ||
1. Visit the project in your web browser at: http://localhost:5000, you should see the current date and time in UTC displayed every time you load the webpage. | ||
1. You can stop running the server by hitting Ctrl+C (not Command+C) in the terminal that foreman is running in. | ||
|
||
#### Running the Django Application on your local machine, using a PostgreSQL database backend, after you've successfully completed all of the instructions above. | ||
1. Create your PostgreSQL database which will only exist on your local machine for development. For example: | ||
1. Create new user for database (optional): ```createuser -U jeremy -deEP app_dateAndTime``` | ||
1. Enter in password, such as: ```myPassword``` | ||
1. Enter in that same password again, such as: ```myPassword``` | ||
1. Create new database owned by a particular user (such as the user just created): ```createdb -e -E UTF8 -h localhost -U app_dateAndTime app_dateAndTime``` | ||
1. Run psql: ```psql``` | ||
1. View the list of databases to see the newly created database: ```\l``` | ||
1. Hit the ```q``` key to quit reading it. | ||
1. Exit psql: ```\q``` | ||
1. Change the ```DATABASE_URL``` environment variable in the ```.env``` file to the appropriate value using the format: ```scheme://user:password@host:port/database```, for example: ```export DATABASE_URL='postgres://app_dateAndTime:myPassword@localhost:5432/app_dateAndTime'``` | ||
1. Source the ```.env``` file, so that the environment variables will exist for when we run ```manage.py```: ```source .env``` | ||
1. Make migrations: ```python manage.py migrate``` | ||
1. Apply the migrations: ```python manage.py migrate``` | ||
1. Run the Django Project using ```foreman```: ```foreman start``` | ||
1. Visit the project in your web browser at: http://localhost:5000, you should see the current date and time in UTC displayed every time you load the webpage. | ||
1. You can stop running the server by hitting Ctrl+C (not Command+C) in the terminal that foreman is running in. | ||
|
||
## The .env file | ||
|
||
This is a file that contains environment variables that will **only be used on your local machine during development**. **Never put this file under source-control of your project**. It's useful to set the environment variable called "PYTHONUNBUFFERED" to true, as this will make the terminal display any strings printed by the Django Project, when you're running it using ```foreman```. | ||
|
||
### Example of .env file's contents | ||
|
||
``` | ||
# DEBUG is a required environment variable. | ||
export DEBUG=true | ||
# TEMPLATE_DEBUG is a required environment variable. | ||
export TEMPLATE_DEBUG=true | ||
######## DATABASE_URL is a required environment variable. ######## | ||
# Option 1. In this case settings.py would find and set the 'NAME' of the database, i.e. the absolute path of the database file. | ||
export DATABASE_URL='sqlite' | ||
# Option 2. In this case settings.py would find and set the 'NAME' of the database, i.e. the absolute path of the database file. This is equivalent to Option 1. | ||
export DATABASE_URL='sqlite://' | ||
# Option 3. In this case, the 'NAME' of the database will be set to the absolute path that's specified in this string. Replace the absolute path with your own. | ||
export DATABASE_URL='sqlite:////Users/jeremy/dev/heroku_django_1_7_9_starter/db.sqlite3' | ||
# Option 4. In this case a PostgreSQL database engine will be used. Change the components of the string to what you need. | ||
export DATABASE_URL='postgres://app_dateAndTime:myPassword@localhost:5432/app_dateAndTime' | ||
################################################################## | ||
# PYTHONUNBUFFERED is an optional environment variable. It determines whether or not output from print statements in the Django project will be displayed in the console when running the Django project using foreman. | ||
export PYTHONUNBUFFERED=true | ||
``` | ||
|
||
## Deploy the app to Heroku | ||
1. Initiate the git repository: ```git init``` | ||
1. Add all directories and files that aren't set in ```.gitignore``` to be ignored: ```git add .``` | ||
1. Modify the ```runtime.txt``` file so that the Python Runtime specified in it is the one that you would like Heroku to use when it runs your Heroku Application. Visit here for more information: https://devcenter.heroku.com/articles/python-runtimes | ||
1. Commit the initial project: ```git commit -m "Initial production-ready project."``` | ||
1. Log into heroku: ```heroku login``` | ||
1. Create your Heroku Application: ```heroku create your-heroku-app-name``` | ||
1. Create a PostgreSQL database on Heroku: ```heroku addons:create heroku-postgresql:hobby-dev``` | ||
1. Set your production ```SECRET_KEY``` environment variable (make this **unique** and **strong**): ```heroku config:set SECRET_KEY='YOUR_SUPER_SECRET_SUPER_STRONG_UNIQUE_PRODUCTION_SECRET_KEY'``` | ||
1. Set your production ```DEBUG``` environment variable (**ensure this is set to false**): ```heroku config:set DEBUG=false``` | ||
1. Set your production ```TEMPLATE_DEBUG``` environment variable (**ensure this is set to false**): ```heroku config:set TEMPLATE_DEBUG=false``` | ||
1. Deploy your Django Project to Heroku: ```git push heroku master``` | ||
1. Apply migrations to PostgreSQL database on Heroku: ```heroku run python manage.py migrate``` | ||
1. Visit the project in your web browser at: http://your-heroku-app-name.herokuapp.com, you should see the current date and time in UTC displayed every time you load the webpage. | ||
1. If the app isn't running on Heroku, then you might need manually scale it to use 1 dyno: ```heroku ps:scale worker=1``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings") | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.db import models | ||
|
||
class DateAndTime(models.Model): | ||
when = models.DateTimeField('Date and Time', auto_now_add=True) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html> | ||
<head> | ||
<title>Date And Time</title> | ||
</head> | ||
<body> | ||
<h1>Date And Time</h1> | ||
<ul> | ||
{% for dateAndTime in dateAndTimes %} | ||
<li>{{ dateAndTime.when }}</li> | ||
{% endfor %} | ||
</ul> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.shortcuts import render | ||
|
||
from .models import DateAndTime | ||
|
||
def home(request): | ||
dateAndTime = DateAndTime() | ||
dateAndTime.save() | ||
|
||
dateAndTimes = DateAndTime.objects.all() | ||
return render(request, 'home.html', {'dateAndTimes': dateAndTimes}) | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
""" | ||
Django settings for {{ project_name }} project. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/1.7/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/1.7/ref/settings/ | ||
""" | ||
|
||
def getBool(myString): | ||
return True if myString.lower() == "true" else False | ||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
import os | ||
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) | ||
|
||
def printAllEnvironVars(): | ||
print "Environment variables:" | ||
for index, each in enumerate(sorted(os.environ.iteritems())): | ||
print index, each | ||
|
||
#printAllEnvironVars() | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ | ||
|
||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = os.environ['SECRET_KEY'] | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = getBool(os.environ['DEBUG']) | ||
|
||
TEMPLATE_DEBUG = getBool(os.environ['TEMPLATE_DEBUG']) | ||
|
||
# Display state of DEBUG mode | ||
if DEBUG: | ||
print "DEBUG mode is on." | ||
else: | ||
print "DEBUG mode is off." | ||
|
||
# Display state of TEMPLATE_DEBUG mode | ||
if TEMPLATE_DEBUG: | ||
print "TEMPLATE_DEBUG mode is on." | ||
else: | ||
print "TEMPLATE_DEBUG mode is off." | ||
|
||
# Hosts | ||
ALLOWED_HOSTS = ['*'] | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = ( | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'myApp', | ||
) | ||
|
||
MIDDLEWARE_CLASSES = ( | ||
'django.contrib.sessions.middleware.SessionMiddleware', | ||
'django.middleware.common.CommonMiddleware', | ||
'django.middleware.csrf.CsrfViewMiddleware', | ||
'django.contrib.auth.middleware.AuthenticationMiddleware', | ||
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', | ||
'django.contrib.messages.middleware.MessageMiddleware', | ||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||
) | ||
|
||
ROOT_URLCONF = '{{ project_name }}.urls' | ||
|
||
WSGI_APPLICATION = '{{ project_name }}.wsgi.application' | ||
|
||
|
||
# Database | ||
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases | ||
|
||
DATABASES = {} | ||
if os.environ['DATABASE_URL'] == 'sqlite' or os.environ['DATABASE_URL'] == 'sqlite://': | ||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
} | ||
} | ||
else: | ||
import dj_database_url | ||
DATABASES['default'] = dj_database_url.config() | ||
|
||
print "default database's ENGINE:", str(DATABASES['default']['ENGINE']) | ||
print "default database's NAME:", str(DATABASES['default']['NAME']) | ||
|
||
# Honor the 'X-Forewarded-Proto' header for request.is_secure() | ||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') | ||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/1.7/topics/i18n/ | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/1.7/howto/static-files/ | ||
STATIC_ROOT = 'staticfiles' | ||
STATIC_URL = '/static/' | ||
|
||
STATICFILES_DIRS = ( | ||
os.path.join(BASE_DIR, 'static'), | ||
) |
Oops, something went wrong.