A skeleton for django framework with the following features:
- Separated admin from apps
apps/admin
- Separated config to use with different environments, main file and extended config files
- Themed admin with
django-jet
- Environment variables based config, including databases as URLs
with
dj_database_url
- Reads environment from
.env
file (dotenv) - Fabric deploy via
fake
and some additional ssh manage tasks - Docker app container via
Dockerfile
withnginx
anduwsgi
- Tornado web server alternative for production environments
- Document generation via
Sphinx
schema
Please rename project-name
with your project folder name
curl -sL https://github.com/fbuccioni/django-rest-skeretonu/tarball/master | tar -zxvf -; mv -v fbuccioni-django-rest-skeretonu-[0-9a-f]*[0-9a-f] project-name
The reason I chose to use the settings
and wsgi
directories outside
the app, is because I need to standarize projects with multiple apps
to make containers or different orchrestration and automation scripts
without the dependency of "a main application" to start the applications.
Because those dirs are in the root folder, I decided to use an /apps
folder in the root to put all apps there and make the skeleton tidy.
Additionally I use the folder /automation
for requierements.txt
and
some other automation stuff like ssh keys, fabfile.py
scripts, other
scripts, etc.
There are 4 ways to configure:
- By default uses
settings.env.default
module, the example ofsettings/env/default.py
is a simpy symlink tolocal.py
(in the same directory) - The common use of
DJANGO_SETTINGS_MODULE
environment variable pointing to your environment, an example can be `settings.env.prod - Dotenv, the
manage.py
and thewsgi/__init__.py
(the wsgi application inwsgi
module) also read the.env
file in the root - Environment Variables using
os.getenv
In my personal experience, I use a module for the static configuration for
different environments and os.getenv
for environment variables and depending
on the envoironment use raw environment variables, for example in a docker
container or .env
if it's a deploy with Fabric/fake
The skeleton uses a Fabric template, but you need fake to use it, try to install globally and not inside your virtualenv.
pip install fake
Fake provides a deploy capistrano-like tasks, check out the Fabric GitHub and additionally have the following commands
Task | Description |
---|---|
cleanup_rollback |
Remove and archive rolled-back release. |
copy_envfile |
Copy dotenv file arg to the main app directory as dotenv |
create_run_dir |
Create a run directory inside the project root for the socket and pid files |
deploy_dir_perms |
Add permissions to the service user and the app user using setfacl Linux |
deploy_staticfiles |
Deploy django static files in the server |
deploy_virtualenv |
Create the virtualenv inside the root project directory |
finished |
Extended Fake task, executes restart job |
install_dotenv |
Copy the dotenv file int the root application directory to current app files |
install_git_ssh |
Installs ssh kit for git (automation/ssh.key) |
install_ssh_key |
Installs ssh public key from key_filename env into authorized_keys file |
install_ssh_kit |
Install the whole ssh kit (key + git config) |
log_dir_permissions |
Create a log directory in the current app files |
migrate_database |
Apply django database migrations in the server |
published |
Extended Fake task, executes log_dir_permissions , deploy_virtualenv , install_dotenv , deploy_staticfiles , migrate_database , create_run_dir |
restart |
Restart service (This case uwsgi using sudo ) |
starting |
Start a deployment, make sure server(s) ready. |
symlink_files |
Symlink linked files. |
symlink_folders |
Symlink linked folders. |
- Add a development key to your git project
- Save the key as
automation/ssh.key
- Install your public key in authorized_keys, maybe
fab -R env install_ssh_key
task can help. - Install the git key using
fab -R env install_git_ssh
- Create the application directory in the server
- Create server app configurations, the example uses uwsgi in the task
restart
maybe you use another - Test it with
fab -R env deploy
- Check if errors
goto 7 until works
The httpserver.py
is a python script to run a
Tornado web server serving our Django project.
Note: If your project use static files, this script use static from
/static
folder, so you must run the collectstatic
django command
before run this command.
Note: You must install the tornado python package before use this
script, this can be done via pip
using the following command:
pip install tornado
python httpserver.py 9000
And the web server will be running in the port 9000
, the default host is
0.0.0.0
binding the service to all ips. you can specify a host with 127.0.0.1:9000
argument.
All suggestions are always welcome, pleas feel free to ask in the issues section of this repo.