Django static customized configuration

STATIC

# APP本地静态资源目录(就APP对应的)
STATIC_URL = "/static/"

# 远程静态文件URL(少用)
REMOTE_STATIC_URL

# 外部引用静态文件目录(外层的)
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

# collect本地静态文件目录(python manage.py collectstatic可以出现的)
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

TEMPLATES

Generally, Django’s default TEMPLATES configuration is:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

        Note: BACKEND: Specify the template engine (generally no need to change). APP_DIRS: A Boolean value indicating whether to look for templates in the templates subdirectory of each installed application. In this example, it's set to True, which means Django will look for templates in each application's templates folder;

        For example, if you externally reference a folder developed by yourself at this time, in a project where the front and back ends are not separated, you need to plug it into DIRS. If you are configuring the front-end environment variables, then plug it directly into the context_processors of OPTIONS.

        If you are using some framework that embeds TEMPLATES into the SDK, you can assign values ​​directly using a dictionary.

        

        For example, you wrote a context_processors.py file in your APP (here called hh_app)

def set_global_variable(request):
    context = {
        "STATIC_URL": settings.STATIC_URL + "dist/",  # 本地静态文件访问
        "APP_ID": settings.APP_ID,  # app id
    }
    return context

The configuration file can be configured as follows:

TEMPLATES[0]["OPTIONS"]["context_processors"] += ("hh_app.context_processors.set_global_variable",)

Guess you like

Origin blog.csdn.net/lxd_max/article/details/133800457