需求背景:由于测试服和生产服的样式相同,导致操作admin后台时,容易误操作;所以打算定制主题,让两者看起来明显不同。
之前就有用过django-admin-interface,感觉还不错,所以这次还是用它
pip install django-admin-interface
大致代码如下:
import os
import re
from pathlib import Path
from typing import List
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "..."
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = BASE_DIR.split("/")[2] != "prod-user-name"
ALLOWED_HOSTS: List[str] = ["xxx", "yyy"]
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
...
]
if DEBUG:
ALLOWED_HOSTS = ["*"]
# https://pypi.org/project/django-admin-interface/
INSTALLED_APPS[:0] = ["admin_interface", "colorfield"]
X_FRAME_OPTIONS = "SAMEORIGIN"
# python manage.py migrate
# python manage.py loaddata admin_interface_theme_bootstrap.json
# python manage.py collectstatic --clear --no-input