【问题标题】:Dashboard doesn't populate values after forking template django-oscars分叉模板 django-oscars 后仪表板不填充值
【发布时间】:2018-06-01 16:44:44
【问题描述】:

这是我的仪表板 view.py-

from django.views.generic import TemplateView
class IndexView(TemplateView):
    def get_template_names(self):
    if self.request.user.is_staff:
      return ['mytemplates/index.html']
    else :
      return ['dashboard/index_nonstaff.html','mytemplates/index.html']

所以我成功地创建了应用程序并扩展了模板,但由于某种原因,仪表板上没有显示任何值

【问题讨论】:

标签: django django-oscar


【解决方案1】:

您定义了一个新的IndexView,它不继承 Oscar 的 IndexView - 因此模板无法找到任何相关上下文。您目前拥有的只是一个普通的TemplateView,与 Oscar 没有任何关系。

假设您已经按照fork the dashboard app 的说明进行操作,那么您需要继承 Oscar 的 IndexView

from oscar.apps.dashboard.views import IndexView as CoreIndexView

# Note - you subclass CoreIndexView, not TemplateView
class IndexView(CoreIndexView):

    def get_template_names(self):
        if self.request.user.is_staff:
            return ['mytemplates/index.html']
        else:
            return ['dashboard/index_nonstaff.html','mytemplates/index.html']

请参阅this bit of the documentation,其中解释了如何执行此操作。

也就是说,如果您只想覆盖模板(而不是任何视图逻辑),那么实际上不需要覆盖视图。只需覆盖 Oscar 的模板 as described here - 这样您就可以将模板 yourproject/templates/dashboard/index.html 添加到您的项目中,该模板将优先于 Oscar 的默认模板加载。

【讨论】:

  • 感谢您的帮助,我已经发布了我所犯的错误 :)
  • 对于它的价值,您的回答实际上并没有解决您最初发布的问题。即使您确实更改了TEMPLATES 设置,您在问题中发布的代码仍然无法正常工作,我已经在回答中解释了原因。
  • 出于某种原因,它正在工作!所以,这不是我面临的问题
【解决方案2】:

我正确地遵循了文档中的所有内容,终于找出了错误。在我的 settings.py 中,我将模板的路径放在了 django-oscars 默认模板之前。

以下是正确方式的sn-p

TEMPLATES = [  
'DIRS': [OSCAR_MAIN_TEMPLATE_DIR, 
'path to/mytemplates', ], 

【讨论】:

    猜你喜欢
    • 2021-08-30
    • 2011-12-27
    • 2019-03-02
    • 2019-11-18
    • 2020-06-21
    • 2017-10-20
    • 1970-01-01
    • 2020-02-29
    • 2018-07-11
    相关资源
    最近更新 更多