【问题标题】:Django Oscar Template override not workingDjango Oscar模板覆盖不起作用
【发布时间】:2019-11-20 09:18:24
【问题描述】:

我面临 django oscar 模板覆盖的问题。我正在使用 Oscar 2.0.3 和 Django 2.2.7

我正在使用文档中提到的方法 1。我的项目结构看起来像

myproject
   cache
   catalogue (I have overridden this app)
   categories 
   images
   templates
      base.html
   myproject
   manage.py

我已将我的TEMPLATES 编辑为


import os
location = lambda x: os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', x)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [location('templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'oscar.apps.search.context_processors.search_form',
                'oscar.apps.checkout.context_processors.checkout',
                'oscar.apps.customer.notifications.context_processors.notifications',
                'oscar.core.context_processors.metadata',
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages', 
            ],
        },
    },
]

我正在对base.html 进行更改,但它们没有在127.0.0.1:8000/catalogue/ 复制(127.0.0.1:8000 重定向到此网址)

感谢您的帮助!谢谢!

【问题讨论】:

  • 我正在尝试重现此视频 youtube.com/watch?v=iFXOx7FOC9k&t=329s 中的行为。当我将 base.html 中的 django-oscar 粘贴到我自己的 base.html 中时,它没有显示任何内容。好像它正在渲染一个空的html。相比之下,视频中遵循的步骤再现了着陆页。
  • 您检查过location('templates') 解析的内容吗?我想你会发现它并不指向你放置模板的目录。您可以通过从设置文件中打印该值来检查。
  • 是的,我做到了。它确实指向我用于本地项目的模板文件夹。我可以渲染base.html,但我必须明确地这样做(通过编写view 并编辑urls.py 以重定向到该视图。我无法在我附加的视频中重现他的行为。@日光烟雾

标签: django-oscar


【解决方案1】:

您使用的教程适用于旧版 Oscar (1.6)。

在 Oscar 2.0 中,模板结构发生了变化。特别是base.html 现在是oscar/base.html。这意味着要覆盖它,您必须在 templates/oscar/base.html 而不是 templates/base.html 创建一个文件。

这同样适用于所有其他 Oscar 模板。

【讨论】:

  • 是的!那是问题!我搞砸了目录结构。非常感谢您的帮助!和一个很棒的项目! @solarissmoke