DjangoHero is the fastest way to set up a Django project on the cloud (using Heroku). Starting a Django project using DjangoHero will
- Set up the Django project with a folder structure that follows best practices.
- Make the Django project live on the cloud (Heroku) immediately.
- Provision a database on the cloud (Heroku postgres) and connect your appication to it.
Best part : all of this happens with a single command under a minute without any input from your side. This is as good as plug and play gets.
To start a Heroku app called my_app
running an empty Django project called my_django_project
, complete with a
database, we need to run the following command. Try it out, it works like a charm.
djangohero deploy --app=my_app --database my_django_project
Creating a Django app on Heroku usually requires the following steps (you don't have to read all of them, just notice how many there are):
- Create a Django project using
django-admin startproject
. - Modify the Django project to conform to best practices outlined in Two Scoops of Django or whatever project structure suits your team best.
- Create a settings file for Heroku and add Heroku specific database settings and static file settings.
- For serving static files from Heroku, modify
wsgi.py
to use whitenoise. - Initialize a Git repository for the project.
- Create the heroku app.
- Add the app url to
ALLOWED_HOSTS
in the Django settings. - Set heroku config vars for
DJANGO_SETTINGS_MODULE
andDJANGO_SECRET_KEY
. If you are using best practices for Django, you'd need these. - Create
requirements.txt
containing required Python packages. - Create a
Procfile
containing information on the web process andruntime.txt
containing information on the preferred Python runtime. - Create a Heroku Postgres addon to set up a database.
- Commit the changes to Git and push the changes to Heroku.
- Scale the app.
These boring steps have to be repeated for every project before we can get it to go live on the cloud. This package solves the problem by automating the setup process and making the application live on the cloud from birth.
The following steps are recommended.
###Git clone this repo
git clone https://github.com/gutfeeling/djangohero.git
###Install with pip
pip install -e djangohero
Installing djangohero will make the program djangohero
available on your system. Here's how to use it.
djangohero deploy --app=<app_name> --region=<region> --scale=<scale> --database --database_type=<database_type> --python=<python_version> --container_name=<container_folder_name> <django_project_name>
--app : Name of the Heroku app; defaults to Heroku's imaginative app names.
--region : Geographical location of the app; defaults to us.
--scale : Scale of the dynos; defaults to 1.
--database : If this flag is present, a database is created.
--database_type : Name of the Heroku Postgres' plan tier; defaults to hobby-dev.
--python : Python runtime (either 2 or 3); defaults to Python 3.
--container_name : Name of the container folder; defaults to app name.
django_project_name : Name of the Django project.
The created app uses the following folder structure for the Django project in accordance with Two Scoops of Django and Heroku's requirements. The folder structure can be changed without much trouble as well by changing the template.
container_name
├── django_project_name
│ ├── django_project_name
│ │ ├── __init__.py
│ │ ├── settings
│ │ │ ├── __init__.py
│ │ │ ├── settings_base.py
│ │ │ └── settings_heroku.py
│ │ ├── urls.py
│ │ └── wsgi
│ │ ├── __init__.py
│ │ ├── wsgi_base.py
│ │ └── wsgi_heroku.py
│ └── manage.py
├── Procfile
├── requirements.txt
└── runtime.txt
Because of the project structure, you need to execute the following commands for migrations
heroku run django_project_name/manage.py makemigrations
heroku run django_project_name/manage.py migrate
Works on both Python 2 and 3.
You should have the Heroku CLI and Git installed for this to work. You need
to logged into Heroku at the time of running djangohero
. You can login to Heroku by using the following command.
heroku login
There's another project called heroku-django-template that provides a Heroku template. It does not fit my use case as the template there does not conform to the best practices outlined in Two Scoops of Django and it doesn't try to automate the other command line things. But it might fit yours, do check it out.
If you think similar stuff can be done for other languages that work on Heroku, we could create a unified project containing all the code. Drop me a line at dibyachakravorty@gmail.com
Finally, your contributions and suggestions to improve this project is most welcome.