3.2. Setup Start Project

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

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

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

3.2.4. Check

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

3.2.5. Assignments

Code 3.94. Solution
# doctest: +SKIP_FILE
"""
* Assignment: Django Conf CreateProject
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

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
    >>> 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()
"""


Code 3.95. Solution
# doctest: +SKIP_FILE
"""
* Assignment: Django Conf Migrate
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min

English:
    1. Migrate the database
    2. Run doctests - all must succeed

Polish:
    1. Wykonaj migrację bazy danych
    2. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from pathlib import Path

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


Code 3.96. Solution
# doctest: +SKIP_FILE
"""
* Assignment: Django Conf Superuser
* Complexity: easy
* Lines of code: 2 lines
* Time: 3 min

English:
    1. Create superuser:
        a. username: `admin`
        b. email: `admin@example.com`
        c. password: `valid`
    2. Restart server
    3. Open browser and goto http://127.0.0.1:8000/admin/
    4. Login as `admin`
    5. Run doctests - all must succeed

Polish:
    1. Stwórz superuser:
        a. username: `admin`
        b. email: `admin@example.com`
        c. password: `valid`
    2. Uruchom serwer:
        a. ip: 127.0.0.1
        b. port: 8000
    3. Otwórz przeglądarkę i przejdź na stronę http://127.0.0.1:8000/admin/
    4. Zaloguj się jako `admin`
    5. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from urllib.request import urlopen
    >>> import sqlite3

    >>> sql = 'SELECT date_joined FROM auth_user WHERE username="admin"'
    >>> with sqlite3.connect('myproject/db.sqlite3') as db:
    ...     result = db.execute(sql).fetchone()
    >>> assert result is not None

    >>> response = urlopen('http://127.0.0.1:8000/admin/')
    >>> result = response.read().decode('utf-8')
    >>> assert result is not None
"""


Code 3.97. Solution
"""
* Assignment: Django Conf RunConfiguration
* Complexity: medium
* Lines of code: 0 lines
* Time: 8 min

English:
    1. Create `Run Configuration` in your IDE:
        a. add new configuration: `Python`
        b. name: `makemigrations`
        c. script: `manage.py`
        d. parameters: `makemigrations`
        e. working directory: `myproject`
        f. environment variables: `DEBUG=True`
    2. Run `makemigrations` configuration
    3. Run Doctests - all must succeed

Polish:
    1. Stwórz w swoim IDE `Run Configuration`:
        a. add new configuration: `Python`
        b. name: `makemigrations`
        c. script: `manage.py`
        d. parameters: `makemigrations`
        e. working directory: `myproject`
        f. environment variables: `DEBUG=True`
    2. Uruchom konfigurację `makemigrations`
    3. Uruchom Doctesty - wszystkie muszą się powieść

    Migrate:
    - Add new configuration: Python
    - script: `manage.py`
    - parameters: `migrate`
    - working directory: `myproject`
    - environment variables: `DEBUG=True`

    (copy configuration)
    Makemigrations:
    - Add new configuration: Python
    - script: `manage.py`
    - parameters: `makemigrations`
    - working directory: `myproject`
    - environment variables: `DEBUG=True`

"""


Code 3.98. Solution
"""
* Assignment: Django Conf RunConfiguration
* Complexity: medium
* Lines of code: 0 lines
* Time: 8 min

English:
    1. Create `Run Configuration` in your IDE:
        a. add new configuration: `Python`
        b. name: `migrate`
        c. script: `manage.py`
        d. parameters: `migrate`
        e. working directory: `myproject`
        f. environment variables: `DEBUG=True`
    2. Run `migrate` configuration
    3. Use `copy configuration`
    4. Run Doctests - all must succeed


Polish:
    1. Stwórz w swoim IDE `Run Configuration`:
        a. add new configuration: `Python`
        b. name: `migrate`
        c. script: `manage.py`
        d. parameters: `migrate`
        e. working directory: `myproject`
        f. environment variables: `DEBUG=True`
    2. Uruchom konfigurację `migrate`
    3. Użyj `kopiowania konfiguracji`
    4. Uruchom Doctesty - wszystkie muszą się powieść

"""