Getting Started With Django the Awesome Way

Have you ever wanted to get a user interface with a cloud database up in 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!

chrome browser with many tabs

Follow these steps and you won’t need all those open Chrome tabs!

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

Eleven Steps To Django Bliss

  1. 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.

  2. Create a project directory where you want to build your project

    mkdir projectname
    cd projectname
  3. Create a virtual environment
    python3 -m venv env

    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!

  4. 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.

  5. 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.

  6. Create a new Django project from the Heroku starter template
    django-admin.py startproject --template=https://github.com/RadialDevGroup/heroku-django-template/archive/master.zip --name=Procfile projectname .

    (replace projectname with your project’s name)
    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.

  7. 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

  8. 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

  9. 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.

  10. 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.

  11. 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”

  1. python3 --version
  2. mkdir myproject; cd myproject
  3. python3 -m venv env
  4. source env/bin/activate
  5. pip install django
  6. django-admin.py startproject --template=https://github.com/RadialDevGroup/heroku-django-template/archive/master.zip --name=Procfile {projectname} .
  7. subl projectname/settings.py
  8. pip install -r requirements.txt
  9. python manage.py migrate
  10. python manage.py createsuperuser
  11. python manage.py runserver

Leave a Reply