A cookiecutter template for Django.
- For Django 1.8
- Renders Django projects with 100% test coverage
- Twitter Bootstrap v4.0.0 - alpha
- End-to-end via Hitch
- AngularJS
- 12-Factor based settings via django-environ
- Optimized development and production settings
- Registration via django-allauth
- Comes with custom user model ready to go.
- Grunt build for compass and livereload
- Basic e-mail configurations for sending emails via Mailgun
- Media storage using Amazon S3
- Serve static files from Amazon S3 or Whitenoise (optional)
- Pre configured Celery (optional)
- Integration with Maildump for local email testing (optional)
- Integration with Sentry for error logging (optional)
- Docker support using docker-compose for dev and prod
- Procfile for deploying to Heroku
- Only maintained 3rd party libraries are used.
- PostgreSQL everywhere (9.0+)
- Environment variables for configuration (This won't work with Apache/mod_wsgi).
Let's pretend you want to create a Django project called "redditclone". Rather than using startproject and then editing the results to include your name, email, and various configuration issues that always get forgotten until the worst possible moment, get cookiecutter to do all the work.
First, get cookiecutter. Trust me, it's awesome:
$ pip install cookiecutter
Now run it against this repo:
$ cookiecutter https://github.com/pydanny/cookiecutter-django.git
You'll be prompted for some questions, answer them, then it will create a Django project for you.
Warning: After this point, change 'Daniel Greenfeld', 'pydanny', etc to your own information.
It prompts you for questions. Answer them:
Cloning into 'cookiecutter-django'... remote: Counting objects: 550, done. remote: Compressing objects: 100% (310/310), done. remote: Total 550 (delta 283), reused 479 (delta 222) Receiving objects: 100% (550/550), 127.66 KiB | 58 KiB/s, done. Resolving deltas: 100% (283/283), done. project_name [project_name]: Reddit Clone repo_name [Reddit_Clone]: reddit author_name [Your Name]: Daniel Greenfeld email [Your email]: [email protected] description [A short description of the project.]: A reddit clone. domain_name [example.com]: myreddit.com version [0.1.0]: 0.0.1 timezone [UTC]: now [2015/01/13]: 2015/08/30 year [2015]: use_whitenoise [y]: n use_celery [n]: y use_maildump [n]: y use_sentry [n]: y windows [n]:
Enter the project and take a look around:
$ cd reddit/ $ ls
Create a GitHub repo and push it there:
$ git init $ git add . $ git commit -m "first awesome commit" $ git remote add origin [email protected]:pydanny/redditclone.git $ git push -u origin master
Now take a look at your repo. Don't forget to carefully look at the generated README. Awesome, right?
The steps below will get you up and running with a local development environment. We assume you have the following installed:
- pip
- virtualenv
- PostgreSQL
First make sure to create and activate a virtualenv, then open a terminal at the project root and install the requirements for local development:
$ pip install -r requirements/local.txt
Then, create a PostgreSQL database and add the database configuration using the dj-database-url
app pattern: postgres://db_owner:password@dbserver_ip:port/db_name
either:
- in the
config.settings.common.py
setting file, - or in the environment variable
DATABASE_URL
You can now run the usual Django migrate
and runserver
command:
$ python manage.py migrate $ python manage.py runserver
Live reloading and Sass CSS compilation
If you'd like to take advantage of live reloading and Sass / Compass CSS compilation you can do so with the included Grunt task.
Make sure that nodejs is installed. Then in the project root run:
$ npm install
Now you just need:
$ grunt serve
The base app will now run as it would with the usual manage.py runserver
but with live reloading and Sass compilation enabled.
To get live reloading to work you'll probably need to install an appropriate browser extension
It's time to write the code!!!
The steps below will get you up and running with a local development environment. We assume you have the following installed:
- docker
- docker-compose
Open a terminal at the project root and run the following for local development:
$ docker-compose -f dev.yml up
You can also set the environment variable COMPOSE_FILE
pointing to dev.yml
like this:
$ export COMPOSE_FILE=dev.yml
And then run:
$ docker-compose up
To migrate your app and to create a superuser, run:
$ docker-compose run django python manage.py migrate $ docker-compose run django python manage.py createsuperuser
If you are using boot2docker to develop on OS X or Windows, you need to create a /data partition inside your boot2docker vm to make all changes persistent. If you don't do that your /data directory will get wiped out on every reboot.
To create a persistent folder, log into the boot2docker vm by running:
$ bootdocker ssh
And then:
$ sudo su $ echo 'ln -sfn /mnt/sda1/data /data' >> /var/lib/boot2docker/bootlocal.sh
In case you are wondering why you can't use a host volume to keep the files on your mac: As of boot2docker 1.7 you'll run into permission problems with mounted host volumes if the container creates his own user and `chown`s the directories on the volume. Postgres is doing that, so we need this quick fix to ensure that all development data persists.
You may notice that some elements of this project do not exactly match what we describe in chapter 3. The reason for that is this project, amongst other things, serves as a test bed for trying out new ideas and concepts. Sometimes they work, sometimes they don't, but the end result is that it won't necessarily match precisely what is described in the book I co-authored.
Scattered throughout the Python and HTML of this project are places marked with "your stuff". This is where third-party libraries are to be integrated with your project.
Want a stable release? You can find them at https://github.com/pydanny/cookiecutter-django/releases
This is what I want. It might not be what you want. Don't worry, you have options:
If you have differences in your preferred setup, I encourage you to fork this to create your own version. Once you have your fork working, let me know and I'll add it to a 'Similar Cookiecutter Templates' list here. It's up to you whether or not to rename your fork.
If you do rename your fork, I encourage you to submit it to the following places:
- cookiecutter so it gets listed in the README as a template.
- The cookiecutter grid on Django Packages.
I also accept pull requests on this, if they're small, atomic, and if they make my own project development experience better.