2.2. Setup Start Project

2.2.1. Create Project

Run in the system terminal:

$ django-admin startproject myproject

Structure:

myproject
├── manage.py
└── myproject
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

2.2.2. Create Database

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

2.2.3. Create Superuser

$ python manage.py createsuperuser
Username (leave blank to use 'admin'): admin
Email address: admin@example.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

2.2.4. Check

$ python manage.py check
System check identified no issues (0 silenced).

2.2.5. Assignments

# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Setup StartProject
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 1. Create a project `myproject`
# 2. Run doctests - all must succeed

# %% Polish
# 1. Stwórz projekt `myproject`
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> from pathlib import Path
>>> basedir = Path('myproject')

>>> assert basedir.exists()
>>> assert (basedir/'manage.py').exists()
>>> assert (basedir/'myproject'/'settings.py').exists()
>>> assert (basedir/'myproject'/'asgi.py').exists()
>>> assert (basedir/'myproject'/'wsgi.py').exists()
>>> assert (basedir/'myproject'/'urls.py').exists()
"""

...

# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Setup Migrate
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 0. Use `myproject`
# 1. Migrate the database
# 2. Run doctests - all must succeed

# %% Polish
# 0. Użyj `myproject`
# 1. Wykonaj migrację bazy danych
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> from pathlib import Path
>>> basedir = Path('myproject')

>>> assert basedir.exists()
>>> assert (basedir/'db.sqlite3').exists()
"""

...

# doctest: +SKIP_FILE
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Django Conf Superuser
# - Difficulty: easy
# - Lines: 1
# - Minutes: 2

# %% English
# 0. Use `myproject`
# 1. Create superuser:
#    - username: `admin`
#    - email: `admin@example.com`
#    - password: `valid`
# 2. Bypass password validation and create user anyway
# 3. Run doctests - all must succeed

# %% Polish
# 0. Użyj `myproject`
# 1. Stwórz superuser:
#    - username: `admin`
#    - email: `admin@example.com`
#    - password: `valid`
# 2. Omiń walidację hasła i stwórz użytkownika
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Output
# - `This password is too short. It must contain at least 8 characters.`
# - `Bypass password validation and create user anyway? [y/N]: y`
# - `Superuser created successfully`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 10), \
'Python 3.10+ required'

>>> import sqlite3

>>> from pathlib import Path
>>> DATABASE = Path(__file__).parent.parent.parent / 'myproject' / 'db.sqlite3'
>>> SQL = 'SELECT date_joined FROM auth_user WHERE username="admin"'
>>>
>>> with sqlite3.connect(DATABASE) as db:
...     result = db.execute(SQL).fetchone()
>>>
>>> assert result is not None
"""

...