【问题标题】:django override admin works locally not on productiondjango override admin 在本地工作而不是在生产中
【发布时间】:2016-12-23 11:09:05
【问题描述】:

我的 django 10 项目中有以下目录结构:

/my-project/ # project dir
    +app1
    +templates
        +admin
            base.html
        404.html
        500.html

我的模板属性在设置中如下所示:

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

我的自定义 base.html 显示在我的本地计算机上。当我将它上传到我的生产服务器时,它不再覆盖并使用项目文件夹中的 base.html 文件。

我已经改变了建议的应用程序here 的顺序,并尝试打印模板属性的 dirs 元素,该元素打印“templates/”,如here

有谁知道我怎样才能让它在我的生产环境中工作?

【问题讨论】:

    标签: python django django-templates django-admin django-settings


    【解决方案1】:

    您必须在设置中使用绝对路径以避免出现问题。例如:

    import os
    
    PROJECT_ROOT = os.path.join(os.path.dirname(__file__), '..', '..')
    # depending where your settings.py live
    
    ...
        'DIRS': [
            os.path.join(PROJECT_ROOT, 'templates'),
        ],
    

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2023-03-19
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      相关资源
      最近更新 更多