在 Django 中设置多个 MEDIA_URL & MEDIA_ROOT

Setting multiple MEDIA_URL & MEDIA_ROOT in django

我在我的 django 应用程序中设置了静态和媒体根以及 url,如下所示:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

MEDIA_URL = '/crl/'
MEDIA_ROOT  = os.path.join(BASE_DIR, 'config/crl/')

它运行良好,但我想添加另一个 MEDIA_URL & MEDIA_ROOT 来提供 /certs/ 目录中的文件,如下所示:

NEW_MEDIA_URL = '/certs/'
NEW_MEDIA_ROOT  = os.path.join(BASE_DIR, 'config/certs/')

有什么办法吗? 我正在使用 Django 2.0.6 和 Python 3.5

可以使用以下步骤将多个静态 URL 和静态根添加到 Django。

  1. 配置一个BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)

  1. 根据需要创建尽可能多的静态根目录和静态 URL
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# the "static/" above is a directory inside the Django Project Directory

STATIC_URL_1 = '/static-1/'
STATIC_ROOT_1 = os.path.join(BASE_DIR, "static_1/")
  1. 同样,您可以根据需要创建任意数量的媒体根目录和媒体 URL
MEDIA_URL = '/crl/'
MEDIA_ROOT  = os.path.join(BASE_DIR, 'config/crl/')

MEDIA_URL_1 = '/crl-1/'
MEDIA_ROOT_1  = os.path.join(BASE_DIR, 'config/crl_1/')