From the frontend to Django: what we add and preparing the tools

Lesson 1 of 9 · Python web: Django project

The frontend of the Logo creator works in the browser, but it remembers nothing: after the browser is closed the logos disappear, and users have no accounts. For that we need a backend – a program on a server with a database. In this part we build it in Python with the Django framework. First: exactly what we add and how to prepare the computer.

Before you start: required knowledge – the frontend project Logo creator (the HTML, CSS and JavaScript course), the basics of Python: functions, dictionaries, classes.
How to learn: work in your own project step by step and after every lesson check that the server works; do the task at the end on your own first. The code is for Django 5.2 (LTS) and Python 3.12+; write each part into the file named in the comment at the top of the example.

What the backend adds to the project

FeatureWithout a backend (frontend)With Django
home page and generatoryesyes – the same HTML, CSS and JavaScript, now as Django templates
user accountsnoregistration, signing in, signing out
saving logosno (only downloading a file)logos are saved in the database and can be seen from any device
administrationnoan admin panel: an overview of users and logos
protection–everyone sees and deletes only their own logos

The workflow: on the home page the user types a name → generator → sign in or register → the logo is saved → the "My logos" page.

Why Django

FlaskDjango
sizesmall – you add everything yourselfbig – "batteries included"
database and modelsan extra librarybuilt in (ORM, migrations)
accounts and signing inan extra librarybuilt in
admin panelnonebuilt in
for learningexcellent for the first stepsexcellent for a whole project

If you have not worked with any Python web framework yet, first go through the short lessons An introduction to Flask and An introduction to Django from the Python area – here we use them on a real project. The terms request, response, route and status code are explained in the lesson The HTTP protocol.

Tools

ToolForCheck in the command line
Python 3.12 or newerthe languagepy --version
pipinstalling packages (comes with Python)py -m pip --version
VS Code + the Python extensionthe editor–
Gitthe history of changes and putting the site online (lesson 9)git --version

On Windows, Python is downloaded from python.org; during installation tick Add python.exe to PATH. The PostgreSQL database is not needed for development – Django uses the built-in SQLite, and we only get PostgreSQL on the server.

A virtual environment and installing Django

Every project has its own virtual environment (.venv) – a separate folder with the packages of that project only, so that versions do not get mixed between projects.

:: Windows, cmd – in the folder where you keep your projects
mkdir logo_kreator_django
cd logo_kreator_django
py -m venv .venv
.venv\Scripts\activate
:: from now on the line starts with (.venv)
python -m pip install --upgrade pip
pip install "django>=5.2,<7"
python -m django --version

On Linux and macOS activation is source .venv/bin/activate. Every time you open a new command-line window, activate the environment again. In VS Code: Ctrl+Shift+P → Python: Select Interpreter → choose the one from .venv.

Version: the examples are written for Django 5.2 (the long-term support version, LTS) and also work in Django 6. On the internet there are many examples for old versions (for example from django.conf.urls import url) – they do not work in new versions.

Video: installing the tools

Video in Serbian with English subtitles – turn on subtitles (CC) in the YouTube player.

A note about the video: the video was recorded for the first version of the project (an older version of Django, Heroku). The ideas are the same, but the code in this lesson differs in details – where they differ, the code on this page is the one that applies.

Practice tasks

For every task there is an example, instructions and a solution.

TASK 01Difficulty: easy

Check the environment

Make a project folder and a virtual environment, install Django and write down the output of the commands python --version, pip list and python -m django --version. Then deactivate the environment (deactivate) and run python -m django --version again. What happened and why?

Common mistakes and tips
  • Forgetting to activate the environment is the most common cause of the error No module named django.
  • The .venv folder is not copied or sent to GitHub – it is made again from requirements.txt.
  • Check code from the internet: if it uses url() or django.conf.urls, it was written for Django 3 or older.

Check your knowledge

Open the quiz

Preparing for Django

1. What does Django add to the Logo creator compared with the frontend?




2. What is a virtual environment for?




3. Which database does Django use for development without any setup?