这可能是绝对初学者的问题,因为我对编程很陌生。我已经搜索了几个小时以寻找合适的解决方案,我不知道还能做什么。
跟随问题。我想要一个显示的视图。例如,我的数据库中的 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非常接近我的问题,但我没有看到有效的解决方案。
谢谢您的帮助。
克里斯
-- 我根据这个线程中提供的解决方案编辑了我的代码和问题描述