【问题标题】:How to add similar posts in Detailview in Django 1.11如何在 Django 1.11 的 Detailview 中添加类似的帖子
【发布时间】:2019-01-12 13:14:47
【问题描述】:

我想在详细页面上的 Class DetailView 中将类似的帖子部分添加到我的博客 django 应用程序

class DetailView(generic.DetailView):
    model = Deal
    template_name = 'deals/detail.html'

【问题讨论】:

  • 欢迎来到 SO!请查看How to Ask 以了解如何发布好问题。你需要更具体。你试过什么?什么不工作?

标签: python django django-templates django-1.11


【解决方案1】:
class DetailView(generic.DetailView):
    model = Deal
    template_name = 'deals/detail.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context[post] = post
        return context

get_context_data 方法负责传递模板中的数据。上下文是一个字典,因此您可以在此字典中添加您的键值并访问它的表单模板。

<html>
<body>
{{post}}
</body>
</html>

您将任何数据添加到您的上下文字典中,您可以使用密钥从模板访问它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多