Django 中的过滤器和 for 循环
Filters and for loops in Django
很抱歉,如果这是一个明显的问题,我是 Django 的新手和 Python。我正在开发一个小型博客应用程序。以下代码从我的数据库中检索所有帖子。我不想在主页中显示所有这些。我只需要其中三个。我确信它是根据文档使用 for 循环和某种过滤器完成的。
https://docs.djangoproject.com/en/1.7/ref/templates/builtins/
views.py
def all(request):
allPosts = Post.objects.all()
context = {'Posts': allPosts}
template = "home.html"
return render(request, template, context)
home.html
<div class="sidebar-module">
<h4>Archive</h4>
<ol class="list-unstyled">
{% for singlePost in Posts %}
<li><a href="#">{{singlePost.title}}</a></li>
{% endfor %}
</ol>
</div>
由于您只查找 3 posts,因此无需提取所有条目,以下内容就足够了:
def all(request):
"""To get only three posts use a slice operation."""
allPosts = Post.objects.all()[:3]
context = {'Posts': allPosts}
template = "home.html"
return render(request, template, context)
您可以在官方文档中找到更多信息:
https://docs.djangoproject.com/en/1.7/topics/db/queries/#limiting-querysets
如果您需要过滤结果,请使用相同的概念,但应用您的过滤器:
https://docs.djangoproject.com/en/1.7/topics/db/queries/#retrieving-specific-objects-with-filters
另一方面,如果您要在同一个 post 上使用所有这些,但只想在同一组的特定部分(比如边栏等)上限制为 3 个,那么您可以像在原始 post 中一样获取它们,然后在模板中循环:
<div class="sidebar-module">
<h4>Archive</h4>
<ol class="list-unstyled">
{% for singlePost in Posts|slice:":3" %}
<li><a href="#">{{singlePost.title}}</a></li>
{% endfor %}
</ol>
</div>
请注意,如果您遇到以下情况,以上内容将导致新的查询
加载问题会更好,在视图中评估您的查询集,将其分配给列表
并使用评估查询:
def all(request):
"""The bellow evaluates the queryset before it hits the template.
So slicing in the template (example before) will not result in a new
Queryset."""
allPosts = list(Post.objects.all())
context = {'Posts': allPosts}
template = "home.html"
return render(request, template, context)
您可以在此处找到有关评估的更多信息:
https://docs.djangoproject.com/en/1.7/ref/models/querysets/#when-querysets-are-evaluated
很抱歉,如果这是一个明显的问题,我是 Django 的新手和 Python。我正在开发一个小型博客应用程序。以下代码从我的数据库中检索所有帖子。我不想在主页中显示所有这些。我只需要其中三个。我确信它是根据文档使用 for 循环和某种过滤器完成的。
https://docs.djangoproject.com/en/1.7/ref/templates/builtins/
views.py
def all(request):
allPosts = Post.objects.all()
context = {'Posts': allPosts}
template = "home.html"
return render(request, template, context)
home.html
<div class="sidebar-module">
<h4>Archive</h4>
<ol class="list-unstyled">
{% for singlePost in Posts %}
<li><a href="#">{{singlePost.title}}</a></li>
{% endfor %}
</ol>
</div>
由于您只查找 3 posts,因此无需提取所有条目,以下内容就足够了:
def all(request):
"""To get only three posts use a slice operation."""
allPosts = Post.objects.all()[:3]
context = {'Posts': allPosts}
template = "home.html"
return render(request, template, context)
您可以在官方文档中找到更多信息: https://docs.djangoproject.com/en/1.7/topics/db/queries/#limiting-querysets 如果您需要过滤结果,请使用相同的概念,但应用您的过滤器: https://docs.djangoproject.com/en/1.7/topics/db/queries/#retrieving-specific-objects-with-filters
另一方面,如果您要在同一个 post 上使用所有这些,但只想在同一组的特定部分(比如边栏等)上限制为 3 个,那么您可以像在原始 post 中一样获取它们,然后在模板中循环:
<div class="sidebar-module">
<h4>Archive</h4>
<ol class="list-unstyled">
{% for singlePost in Posts|slice:":3" %}
<li><a href="#">{{singlePost.title}}</a></li>
{% endfor %}
</ol>
</div>
请注意,如果您遇到以下情况,以上内容将导致新的查询 加载问题会更好,在视图中评估您的查询集,将其分配给列表 并使用评估查询:
def all(request):
"""The bellow evaluates the queryset before it hits the template.
So slicing in the template (example before) will not result in a new
Queryset."""
allPosts = list(Post.objects.all())
context = {'Posts': allPosts}
template = "home.html"
return render(request, template, context)
您可以在此处找到有关评估的更多信息: https://docs.djangoproject.com/en/1.7/ref/models/querysets/#when-querysets-are-evaluated