【问题标题】:Django pass render_to_response template in other templateDjango 在其他模板中传递 render_to_response 模板
【发布时间】:2015-10-25 17:34:34
【问题描述】:

这可能是绝对初学者的问题,因为我对编程还很陌生。我已经搜索了几个小时以寻找合适的解决方案,我不知道还能做什么。

以下问题。我想要一个显示的视图。例如我数据库中的 5 个最新条目和 5 个最新条目(仅作为示例)

#views.py
import core.models as coremodels

class LandingView(TemplateView):
    template_name = "base/index.html"

    def index_filtered(request):
        last_ones = coremodels.Startup.objects.all().order_by('-id')[:5]
        first_ones = coremodels.Startup.objects.all().order_by('id')[:5]
        return render_to_response("base/index.html", 
        {'last_ones': last_ones,   'first_ones' : first_ones})  

Index.html 显示 HTML 内容,但不显示循环内容

#index.html

<div class="col-md-6">
    <p> Chosen Items negative:</p>
    {% for startup in last_ones %}
        <li><p>{{ startup.title }}</p></li>
    {% endfor %}
</div>

<div class="col-md-6">
    <p> Chosen Items positive:</p>
    {% for startup in first_ones %}
       <li><p>{{ startup.title }}</p></li>
    {% endfor %}

这是我的问题:

如何让 for 循环呈现特定内容?

我认为Django show render_to_response in template 非常接近我的问题,但我没有看到有效的解决方案。

感谢您的帮助。

克里斯

-- 我根据此线程中提供的解决方案编辑了我的代码和问题描述

【问题讨论】:

  • 调用render_to_response("base/showlatest.html"... 呈现base/showlatest.html,而不是index.html。负责渲染index.html 的视图应该将所有数据(last_onesfirst_ones)传递给它

标签: python django django-templates views render-to-response


【解决方案1】:

调用render_to_response("base/showlatest.html"... 呈现base/showlatest.html,而不是index.html

负责渲染index.html 的视图应该将所有数据(last_onesfirst_ones)传递给它。

将模板包含在index.html中后

{% include /base/showlatest.html %}

更改上面的视图(或创建一个新视图或修改现有视图,相应地更改urls.py)以将数据传递给它

return render_to_response("index.html", 
{'last_ones': last_ones,   'first_ones' : first_ones})

概念是视图渲染一定的模板(index.html),成为返回给客户端浏览器的html页面。 那个是应该接收特定上下文(数据)的模板,以便它可以包含其他可重用的部分(例如showlatest.html)并正确呈现它们。

include 命令只是将指定模板 (showlatest.html) 的内容复制到当前模板 (index.html) 中,就好像它是输入的一样。

因此,您需要调用 render_to_response 并将您的数据(last_onesfirst_ones)传递给负责渲染包含 showlatest.html 的模板的每个视图

抱歉措辞扭曲,有些事情做起来比解释容易。 :)

更新

您上次的编辑澄清您正在使用 CBV(基于类的视图)。

那么你的观点应该是这样的:

class LandingView(TemplateView):
    template_name = "base/index.html"

    def get_context_data(self, **kwargs):
        context = super(LandingView, self).get_context_data(**kwargs)
        context['last_ones'] = coremodels.Startup.objects.all().order_by('-id')[:5]
        context['first_ones'] = coremodels.Startup.objects.all().order_by('id')[:5]
        return context

注意:我个人会避免依赖数据库设置的id 来对记录进行排序。

相反,如果您可以更改模型,请添加一个字段来标记它的创建时间。例如

class Startup(models.Model):
    ...
    created_on = models.DateTimeField(auto_now_add=True, editable=False)

那么在你看来查询可以变成

    def get_context_data(self, **kwargs):
        context = super(LandingView, self).get_context_data(**kwargs)
        qs = coremodels.Startup.objects.all().order_by('created_on')
        context['first_ones'] = qs[:5]
        context['last_ones'] = qs[-5:]
        return context

【讨论】:

  • 首先,感谢您的快速回复。我会赞成它,但我还没有足够的声誉点来做到这一点。我想我明白你的意思。我将 showlatest.html 的代码放入 index.html,将 index_filtered 函数放入我的索引视图,并将渲染从 base.html 更改为 index.html(我更新了 OP 中的所有内容)。不幸的是,它仍然不起作用。有什么我遗漏或理解错误的吗?
  • 没有必要丢弃showlatest.html,包含很好,如果您要在另一个模板中使用它,这是可行的方法。
  • ehm .. 您省略说明您计划使用 CBV(基于类的视图)。如果是这样,您认为何时会调用index_filtered?您应该覆盖get 甚至更好的get_context_data。使用 this great website 获取有关 CBV 的帮助
  • 太棒了,非常感谢。现在就像一个魅力!我想了解 CBV 和 Django 中的其他基础知识还有很多工作要做;)
猜你喜欢
  • 2012-03-01
  • 2013-12-16
  • 2015-10-29
  • 2017-11-05
  • 2017-03-10
  • 1970-01-01
  • 2021-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多