vsupalov

Quick Django Refresher Crash Course

Sometimes, I need a quick reminder of how certain aspects around Django work. My memory is pretty lazy when it comes to details, so after being away from Django for a while I have to revisit the basics every now and then. That’s especially the case when starting a new project.

Usually, this means finding the right parts of the official tutorial and docs. A quick reminder is more than enough, but getting to the right place always means doing the same searches to find the same things..

No more! This is a list of the most important basics - with quick snippets and links about the most fundamental parts of working with Django. It’s meant to help anybody who is starting a new project or getting back into Django after a break.

Let’s go through the most important steps and components which you need to look at when starting a new barebones Django project and getting it to run locally., Where possible, you’ll find links to more in-depth resources containing additional information related to each task.

Install Python and Pipenv

If on a fresh machine, I set up Python 3 the usual way. In addition, I opt for using pipenv for new projects, as the workflow feels nice and smooth.

$ pip3 install pipenv

It can be installed using any version of pip, and will take care of the busywork of handling virtualenvs. If you prefer to go with virtualenvwrapper, that’s fine as well.

Create the Environment

I prefer to start from scratch, to keep things as simple and minimalistic as possible in the beginning.

$ mkdir PROJECTNAME
$ cd PROJECTNAME

$ pipenv --three
# or for a specific version:
# pipenv --python 3.6

$ pipenv install django

This will create a Pipfile in the new directory, and the --three flag makes sure that it’s Python 3. Then we install the most recent stable version of Django, and we’re good to go!

Start Your New Django Project

First, we need to “activate” the virtual environment managed by pipenv:

$ pipenv shell

Now, we can use the installed Django version to start a new project:

$ django-admin startproject PROJECTNAME .

This will tell the startproject command to work in the current directory, but to use the PROJECTNAME to populate the newly created files. The result is, that you don’t have one more directory for the new Django project - instead the manage.py and everything else is created right here, keeping the directory tree shallow.

Of course, you can also use a template project, instead of starting from scratch. This way, you can skip most of the steps ahead.

As a sidenote: now is a good time for an initial Git commit!

Create the First App

A Django project needs an app. So you can go ahead and create one:

$ python manage.py startapp APPNAME

Time to Adjust Your Project Settings

Add your app name to the INSTALLED_APPS list. Edit the TEMPLATES settings, so DIRS contains a 'templates' string - this way you will be able to put your project’s template files into that directory.

INSTALLED_APPS = [
    'django.contrib.admin',
# ...
    'APPNAME',
]

# other settings...

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ["templates"],
# ...

Create Models in Your App

Time to add some content to the models.py of your newly created app:

While we’re at it, we also might add them to the admin. Add the following inside an admin.py file:

from django.contrib import admin
from .models import YourModel

admin.site.register(YourModel)

Migrations

Now that your models exist, you can create initial migrations and apply them:

$ python manage.py makemigrations
$ python manage.py migrate

Create a Superuser

To access the admin, create your login credentials:

$ python manage.py createsuperuser

Run the Development Server

$ python manage.py runserver

Even without views, you can navigate to the admin page and add some data to your new project.

Create Django Templates

Now, you could create a basic HTML template for your site, to be used with future views. Check out the Django Girls tutorial section for a quick example.

Read this if you want more options on spicing up the look of your site.

Write Views

If in doubt, go with non-generic class based views. You can see a comparison here.

Once your view is written, you can add more templates and adjust the urls of your project. Here’s a basic view:

from django.shortcuts import render
from django.views import View

class DummyView(View):
    def get(self, request):
        template_name = 'base.html'
        data = {}
        return render(request, template_name, data)

You’re Good to Go!

By now, you have touched the most important parts of your new projects, and have gotten things to work.

I hope this overview was enough to help you go through the basic steps of starting a Django project from scratch and finding your way into it.

There’s a lot which can be done from here:

I hope this will be useful to you, the next time you have to get back into Django and want to save a little time on navigating to relevant tutorial/doc sections.

If you liked this article, go ahead and sign up to my mailing list below to be notified about similar helpful articles in the future!.

Here are some links which are a good starting point in general:

Want to create better Django apps?

Subscribe for regular articles about Django!

     (About the content, privacy, analytics and revocation). 

    We won't send you spam. Unsubscribe at any time.

    Powered By ConvertKit