这是我的仪表板 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']
因此,我成功地 fork 了应用程序并扩展了模板,但由于某种原因,仪表板上没有显示任何值
最佳答案
您定义了一个新的 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其中解释了如何执行此操作。
也就是说,如果您只想覆盖模板(而不是任何 View 逻辑),那么实际上没有必要覆盖 View 。只需覆盖 Oscar 的模板 as described here - 这样您就可以将模板 yourproject/templates/dashboard/index.html
添加到您的项目中,该模板将优先于 Oscar 的默认模板加载。
关于django - fork 模板 django-oscars 后,仪表板不填充值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50647838/