Helpers / Grade 1 / Django

Django Fast Settings

Обзор настроек часть 1


Обзор настроек часть 2

templates

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


static

STATIC_URL = 'static/'
STATICFILES_DIRS = [BASE_DIR / 'static']

OR

STATIC_URL = 'static/'
if DEBUG:
    STATICFILES_DIRS = (BASE_DIR / 'static',)
else:
    STATIC_ROOT = BASE_DIR / 'static'


mediafile

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


Allowed Host

ALLOWED_HOSTS = ['*']  (временно разрешаем все хосты)


email backend

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

from django.core.mail import send_mail

subject = 'Subject of the Email'
message = 'Body of the Email'
from_email = 'your-email@example.com'
recipient_list = ['recipient@example.com']

send_mail(subject, message, from_email, recipient_list)

Environ

pip install django-environ

import environ

env = environ.Env()

BASE_DIR = ...

environ.Env.read_env(BASE_DIR / '.env')

env = environ.Env(
    DEBUG=(bool, False),
                  )

пример синтаксиса .env

ALLOWED_HOSTS=127.0.0.1,localhost,188.68.220.202
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS')


Django - Q

pip install django-q


INSTALLED_APPS = (
    # other apps
    'django_q',
)


Q_CLUSTER = {
'name': 'your_project_name',
'workers': 4,
'recycle': 500,
'timeout': 60,
'retry': 300,
'queue_limit': 50,
'bulk': 10,
'orm': 'default',
'catch_up': False,
}


python manage.py qcluster


Create daemon:

sudo nano /etc/systemd/system/qcluster.service

[Unit]
Description=Django-Q Cluster Service
After=network.target

[Service]
User=your_username
WorkingDirectory=/path/to/your/django/project
ExecStart=/path/to/python_executable /path/to/manage.py qcluster
Restart=always

[Install]
WantedBy=multi-user.target

sudo systemctl daemon-reload

sudo systemctl enable qcluster

sudo systemctl start qcluster

sudo systemctl status qcluster