Getting Started With Django the Awesome Way

Have you ever wanted to get a user interface with a cloud database up before lunch? If so, Django is a great web framework for you.

But getting that far that quickly means you need to know what you’re doing.

There are many tutorials for starting a Django project, but I always find that most omit a few key steps that they assume you already know how to do. As a developer getting Django projects up and running, I often wind up cross-referencing different tutorials to make sure I don’t miss a step.

So here it is, the master Django tutorial. Now you can have only one Chrome tab open instead of five while you start your next Django project. You’re welcome!

This is for a new Django project in Python 3 for deployment on Heroku.

Eleven Steps To Django Bliss

Check your python version: python3 --version

You should have at least 3.4 but it’s a good idea to go ahead and get the latest version (at the time of this writing 3.6.3). If you find you don’t have python 3 you can download the latest here. Installing a new version of python does not replace your existing python version. You can safely have several versions installed on the same system. As a bonus, python 3.6 introduces a new standard library called secrets that has useful functions for generating secure random tokens.

This creates a virtual environment in your current directory called “env” (or whatever you would like to name it) that will be where all your packages are saved. Before Python 3.3 we would have to use a package called virtualenv, but now it’s built in!

Activate your virtual environment: source env/bin/activate

Now any calls to python will use the version that you used to create the environment (from step 1). Also, any packages you install will be saved to env/lib.

Install Django: pip install django

Pip is the python package manager (similar to bundler on Ruby or npm for Javascript). It is also built into python as of version 3.4.

This will install our forked version of Heroku’s starter template into the current directory (that’s the dot). It provides a common set of settings for a typical project. Our version imports settings from settings.yml which you can exclude from your repository to securely store secret tokens and local settings.

Verify settings: subl projectname/settings.py

Before getting started you should always review the project settings to make sure they are appropriate for your project. You will also want to create a settings.yml with any keys you need. If you want to override a setting in settings.yml you can do something like env.get('TIME_ZONE', 'UTC'). This will use the TIME_ZONE key in your settings.yml and fall back to UTC if it is not defined

Install other packages: pip install -r requirements.txt

This will install all of the packages listed in requirements.txt including things needed for Heroku such as the whitenoise static asset system. As you add dependencies to your project you can add them to requirements.txt by installing them with pip and then running pip freeze > requirements.txt

Run migrations: python manage.py migrate

Django comes with a couple of models to manage future migrations and support user logins for the admin site. This is a command you will need to run every time you make a change to the models. To generate the migrations after changing your model files run python manage.py make migrations. I personally alias m=python manage.py in my ~/.bash_profile.

Create a superuser for the admin site: python manage.py createsuperuser

Follow the prompts to set up a user on your local machine that will be able to log into the admin site.

Start the server!: python manage.py runserver

Hooray! You’ve done it! The server should start up on localhost:8000 by default.

Next steps: if you are not familiar with Django, checkout the Writing Your First Django App tutorial. Pat yourself on the back, you’ve already done the first few commands!

Expert Mode

Here’s a list of just the steps, for those of you who are in “Expert mode”