Django

Siehe auch

Installation und Konfiguration

Django benötigt Python 3.10+.

dnf -y install python3-pip

python3 --version

Virtuelles Environment erzeugen, z.B. django-venv:

python3 -m venv django-venv
source django-venv/bin/activate

Django und einige Module im Virtual Environment installieren:

pip install --upgrade pip wheel setuptools
pip install Django django-appconf requests

django-admin --version

Ein neues Projekt erstellen:

django-admin startproject my_project
cd my_project

Ein neues App im Projekt erstellen:

cd my_project
python manage.py startapp my_app

Zugriff aufs Django von überall erlauben:

my_project/settings.py
ALLOWED_HOSTS = [ '*' ]

MariaDB als Datenbank eintragen:

my_project/settings.py
DATABASES = {
   'default': {
       'ENGINE': 'django.db.backends.mysql',
       'NAME': 'django-db',
       'USER': 'django-user',
       'PASSWORD': 'linuxfabrik',
       'HOST': '192.0.2.10',
       'PORT': '3306',
   }
}
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver

Später genügt:

source django-venv/bin/activate
cd my_project
python manage.py runserver

Admin-Benutzer erstellen:

python manage.py createsuperuser

Models

DB-Model-Definitionen:

from django.db import models

class Contact(models.Model):

    name2 = models.CharField(
        blank=True,
        max_length=100,
        null=True,
    )
    type = models.IntegerField(
        choices=TYPE_CHOICES,
    )
    parent_company = models.ForeignKey(
        'self',
        blank=True,
        null=True,
        on_delete=models.CASCADE,
        related_name='employees',
    )

Felder:

models.IntegerField()
models.BigIntegerField()
models.PositiveIntegerField()

models.FloatField()

models.BooleanField()

models.CharField()
models.TextField()

models.DateField()
models.DateTimeField()
models.TimeField()

models.EmailField()

models.ForeignKey()
models.ManyToManyField()
models.OneToOneField()

Attribute:

blank=False,
choices=[()],
default=100,
limit_choices_to={'contact_type_id': 1},
max_length=10,
null=True,
on_delete=models.CASCADE,
on_delete=models.SET_NULL,
primary_key=True,
related_name='contact_id_invoice',
unique=True,
null=True,

DB-Migrationen zurücksetzen

Frische Datenbank gewünscht?

find . -path "*/migrations/*.py" -not -name "**init**.py" -delete
find . -path "*/migrations/*.pyc" -delete

Anschliessend Datenbank löschen.

Datenbank neu erzeugen:

python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser

Systemd Unit-File

Wenn man Django direkt durch Python statt durch einen Webserver ausführen lassen möchte (also per python manage.py runserver 0.0.0.0:8000), kann man sich folgendes Unit-File erstellen:

/etc/systemd/system/django.service
[Unit]
Description=My Django App
Requires=network.target
After=network.target syslog.target

[Service]
ExecStart=/path/to/venv/django-venv/python manage.py runserver 0.0.0.0:8000
WorkingDirectory=/path/to/django
KillSignal=SIGQUIT
NotifyAccess=all
Restart=always
RestartSec=10
RuntimeDirectory=/path/to/django
StandardError=syslog
TimeoutStartSec=0
Type=simple

[Install]
WantedBy=multi-user.target

Built on 2024-07-16