【发布时间】: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_ones和first_ones)传递给它
标签: python django django-templates views render-to-response