Django | Cookbook



Install current Version (3.2.8)

❯ pip install django==3.2.8

Install next Version (4.0)

❯ pip install --pre django

Check installed version

❯ python -m django --version
❯ django-admin.exe version

First steps

The following steps are based on a summary of the Django Tutorial

Create project

django-admin startproject main
cd working_with_django
python migrate
python runserver 8080
python startapp app_base

Create view

Create view in app_base/

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

Add view to app_base/

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),

Add urls to project main/

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('app_base/', include('app_base.urls')),

Create admin user

$ python createsuperuser
Username (leave blank to use 'user'): admin
Email address: admin@localhost
Password (again): 
Superuser created successfully.

Create data and database

Create database model in app_base/

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Activating models in main/


$ python makemigrations app_base
$ python sqlmigrate app_base 0001

Make app modifiable in the admin (app_base/

from django.contrib import admin
from .models import Question

Writing more views

Create views in app_base/

def detail(request, question_id):
    return HttpResponse("You're looking at question

def results(request, question_id):
    response = "You're looking at the results of question
    return HttpResponse(response

def vote(request, question_id):
    return HttpResponse("You're voting on question

Add new views into app_base/

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),

    path('<int:question_id>/', views.detail, name='detail'),
    path('<int:question_id>/results/', views.results, name='results'),
    path('<int:question_id>/vote/',, name='vote'),

Add template in app_base/templates/polls/index.html

        <li><a href="/polls/{{ }}/">{{ question.question_text }}</a></li>

    <p>No polls are available.</p>

Modify view in app_base/

from django.shortcuts import render
def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    context = {'latest_question_list': latest_question_list}
    return render(request, 'polls/index.html', context)

Raising a 404 error in app_base/

from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404

from .models import Question
# ...
def detail(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'polls/detail.html', {'question': question})

Create template app_base/templates/polls/detail.html

<h1>{{ question.question_text }}</h1>

    <li>{{ choice.choice_text }}</li>


Removing hardcoded URLs in app_base/templates/polls/index.html

   <a href="

The way this works is by looking up the URL definition as specified in the app_base/

# the 'name' value as called by the 
path('<int:question_id>/', views.detail, name='detail'),

Namespacing URL names in app_base/

app_name = 'app_base'

urlpatterns = [

Then, modify link in app_base/templates/polls/index.html

from url ‘detail’ to url ‘app_base:detail’

    <a href="

Use generic views: Less code is better

Create class in app_views/

class HomeView(generic.TemplateView):
    template_name = 'index.html'

Create template app_views/templates/index.html

<h1>App Views:</h1>

Modify app_views/

urlpatterns = [
    path('', views.HomeView.as_view(), name='home'),

Add another app to main project

Create app

$ python startapp app_view

Modify main/

urlpatterns = [
    path('app_base/',  include('app_base.urls')),
    path('app_views/', include('app_views.urls')),

Add data model in app_views/

from django.db import models

class DataItem(models.Model):
    text = models.CharField(max_length=200)
    data = models.IntegerField(default=0)

    def __str__(self):
        return self.text

Register data in app_views/

from django.contrib import admin
from .models import DataItem

Activate models

$ python makemigrations app_views
$ python sqlmigrate app_views 0001
$ python migrate app_views

Navigation / Redirection

Set root page of Django project

When accessing your Django project, the root page will normaly doesn’n show your app homepage.

To change this, you hate to modiy the url handling.

In the following sample, replace <appname> with the name of your app

Define a redirection view in your app (/<appname>/

def redirect_to_home(request):
    return redirect('/<appname>')

Define path in the global (/main/

from django.contrib import admin
from django.urls import include, path
from django.shortcuts import redirect

from <appname> import views

urlpatterns = [
    path('',            views.redirect_to_home, name='home'),
    path('<appname>/',  include('<appname>.urls')),

Highlight current page in navigation menu

<div class="list-group">
    <a href="
            Basic Upload
    <a href="
            Progress Bar Upload

Using PostgresSQL Database

Install PostgresSQL

Create Superuser

createuser.exe --interactive --pwprompt


Additional reading



Blogs and Posts

Resolving problems

Wrong template is used

The template system is using a search approach to find the specified template file, e.g. ‘home.html’.

If you created more than one apps with the same filenames for templates, the first one will be used.

Change the template folders and add the app name, e.g.


Resolving error messages and erors

‘app_name’ is not a registered namespace

One reason for this error is the usage of a namespace in a link.

Back to <a href="

If you want to use this way of links, you have to define the namespace/appname in your <app>/ file

app_name = 'app_views'
urlpatterns = [
    path('', views.HomeView.as_view(), name='home'),

dependencies reference nonexistent parent node

  • Recreate database and migration files
  • Remove all migration files under */migrations/00*.py
  • Remove all pycache folders under */__pycache__ and */*/__pycache__
  • Run migration again
$ python makemigrations
$ python manage migrate

ValueError: Dependency on app with no migrations: customuser

$ python makemigrations

Project Structure

Running tasks with Makefile


	grep -E ':\s+#' Makefile

clearcache:	# Clear Cache
	python3 clearcache

run:		# Run Server
	python3 runserver 8000

deploy:		# Deploy
	rm -rf dist $(PREFIX_PKG)*
	rm -rf polls.dist
	cd polls && python3 sdist
	mkdir polls.dist && mv polls/dist/* polls/$(PREFIX_PKG)* polls.dist

install_bootstrap:	# Install Bootstrap Library
	cd .. && yarn add bootstrap
	rm -rf  polls/static/bootstrap
	mkdir   polls/static/bootstrap
	cp -R ../node_modules/bootstrap/dist/* polls/static/bootstrap

install_jquery:		# Install jQuery Library
	cd .. && yarn add jquery
	rm -rf polls/static/jquery
	mkdir  polls/static/jquery
	cp ../node_modules/jquery/dist/* polls/static/jquery

install_bootstrap_from_source:	# Install Bootstrap from Source
	mkdir -p install && \
	wget -O install/ && \
	unzip install/ -d polls/static/bootstrap/4.1.3