0
我有一个显示各种项目的配置文件模型。 其中之一是国家连接到配置文件。Django视图是否可以只为特定的model.item选择前四个元素?
这里是在视图中发生了什么:
class ProfilePartnerListView(FormMixin, BaseProfilePartnerView, ListView):
model = ProfilePartner
context_object_name = 'profile_list'
view_url_name = 'djangocms_partner_profile:profile-list'
def get(self, request, *args, **kwargs):
context = {}
self.object_list = self.get_queryset().order_by('-date_created')
context.update(self.get_context_data(**kwargs))
context[self.context_object_name] = context['object_list']
country_for_articles = Country.objects.exclude(regions_partner_profile=None).order_by('name')
industries_qs = ProfilePartnerIndustry.objects.active_translations(
get_language()).order_by('translations__name')
budget_qs = ProfilePartner.objects.values_list('budget',
flat=True).distinct()
context['load_more_url'] = self.get_load_more_url(request, context)
context['regions_list'] = country_for_articles
context['industry_list'] = industries_qs
context['budget_list'] = budget_qs
return self.render_to_response(context)
我知道,比如“regions_list”,怎么回事,从它只有4个元素。 但事情是,我的主要对象profile_list“我在模板中使用的渲染,显示的是当我做项目的所有国家:
{% for profile in profile_list %}
{% for country in profile.regions.all %}
<div class="col-xs-12">{{ country }}</div>
{% endfor %}
{% endfor %}
而且一些配置文件了5或6国家。我只想显示第一个4. 有没有办法做到这一点?
非常感谢!
ps:region_list
,industry_list
和budget_list
用于类别,它与我在这里想要的无关。
当您使用切片,如果我叫'{%forloop.last%}'这将是第四个元素为井? –
好吧,(抱歉)! 非常感谢AKS :) –
我很乐意提供帮助。请接受答案,如果它解决您的查询。 – AKS