Cookiecutter and Django – A Cheatsheet

Warning: this is a draft / living document and likely to be updated as I remember.

My ‘go to’ language for nearly everything these days is Python. Most of the applications I’ve built over the years have been really quick, simple things that didn’t require much building, so as a general rule I’ve automatically used Flask (which I’d still recommend for quick, simple web development work). However, there are times where Flask doesn’t quite cut it – more involved applications need more work, and a bigger, more complex framework. Enter Django, a Python framework that lets you build out exactly that.

While it’s actually pretty simple to use once you have the hang of it, I’ve found that the initial setup of Django is not always fun. It’s hard to remember the exact incantations needed to create a project, and there’s a bunch of things where you’re likely just repeating the same old instructions every time you start afresh.

Getting your environment ready

It’s worth making sure that you’re using a virtual environment in pretty much any instance of creating a python project. If you haven’t already, install virtualenv and set up your virtual environment.

The commands below will:

  • create a virtual environment in the venv directory, using Python 3
  • install the latest version of django
  • install cookiecutter (a tool that allows us to install a preconfigured set of applications and settings with our django project)

virtualenv -p python3 venv

source venv/bin/activate should enter your virtual environment. You’ll see this signified by the prompt changing, displaying `(venv)` at the beginning.

pip install django
pip install cookiecutter

You’ll also need a database to store your data in. We’ll automatically be using postgres here, so make sure that’s installed (brew uninstall --force postgresqlis likely the easiest way if you’re on a mac).

Cookiecutter

Cookiecutter is a neat little tool that lets you create projects from prebuilt templates, or “cookiecutters”. I use it to install a prebuilt setup of Django that seems like a pretty nice way to get up and running. Let’s do that now!

cookiecutter https://github.com/pydanny/cookiecutter-django

That’ll ask for some values as it goes; if you’re unsure of any of them, just accept the defaults. It’s worth noting that the project slug section is more important than it mentions here. Make sure you keep a note of it, you’ll be needing that later!

Once everything is installed, we need to make sure that the pip requirements are installed.

pip install -r requirements/local.txt

Setting up your Database

Remember that I mentioned that it’s worth keeping a note of the slug from the cookiecutter install? Yeah, you’re going to need that now. You kept a note, right?

Start your engine postgres server:

pg_ctl -D /usr/local/var/postgres start

Once the server is up, you’ll need to create a new database for your project. Cookiecutter assumes that the database will use the same slug name as your project.

From your shell prompt:

psql
CREATE DATABASE [slug name];
/exit

Once that’s done, make sure you run migrations to make sure that everything is up to date. From the python project folder:

python manage.py migrate

You can now make your superuser.

Checklist

  • pip install virtualenv
  • Activate virtualenv
  • pip install django
  • pip install cookiecutter
  • cookiecutter https://github.com/pydanny/cookiecutter-django
  • Don’t forget to make a note of the slug!
  • pip install -r requirements/local.txt
  • Create the database
  • Run migrations with python manage.py migrate
  • Create a superuser with python manage.py createsuperuser

Leave a Reply

Your email address will not be published. Required fields are marked *