【问题标题】:Template Inheritance doesnot work in Django模板继承在 Django 中不起作用
【发布时间】:2021-01-10 18:01:46
【问题描述】:

我有 base.html(这是我的主页)。它在 navbar.html 中有导航栏,在 base.html 本身中有一些滑块。我创建了渲染上下文(即产品)的 ProductList 视图,该视图在 product_list.html 中用于使值动态化。

现在我的问题是我不想在我的 product.urls 中包含我的 ProductList 视图,因为我不希望它在新页面中呈现。相反,我想在 base.html 中包含 product_list.html,以便在我的主页中显示它,而不是像在普通电子商务网站中那样显示在产品页面中。

所以,我没有在 product.urls 中包含 ProductList,而是在 base.html 中使用包含模板标签包含了该模板。但它没有显示我的产品。当我尝试在单独的页面中呈现产品时,它可以工作。

有什么方法可以做到吗?

【问题讨论】:

    标签: html django-templates templatetags template-inheritance viewrendering


    【解决方案1】:

    我认为您忘记传递上下文。在您的views.py中,将上下文传递给呈现base.html的函数

    例如:

    def index(request):
        context = {'product':products}  #Pass your products
        return render(request,'base.html',context)
    

    【讨论】:

      猜你喜欢
      • 2021-12-10
      • 1970-01-01
      • 2015-11-13
      • 2020-12-13
      • 2011-12-24
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多