最初に最新の説明を取得してから、説明の内容順に並べて表示します...
views.pyに次のものがあります
def category(request, category_name_slug):
"""Category Page"""
context_dict = {}
try:
category = Category.objects.get(slug=category_name_slug)
subcategory = SubCategory.objects.filter(category=category)
websites = Website.objects.filter(sub_categories=subcategory, online=True, banned=False)
sites = websites
descriptions = WebsiteDescription.objects.prefetch_related("about")
descriptions = descriptions.filter(about__in=sites)
descriptions = descriptions.order_by('about', '-updated')
descs = []
last_site = "" # The latest site selected
# Select the first (the latest) from each site group
for desc in descriptions:
if last_site != desc.about.id:
last_site = desc.about.id
desc.url = desc.about.url
desc.hs_id = desc.about.id
desc.banned = desc.about.banned
desc.referral = desc.about.referral
descs.append(desc)
context_dict['descs'] = descs
context_dict['websites'] = websites
context_dict['subcategory'] = subcategory
context_dict['category'] = category
except SubCategory.DoesNotExist:
pass
return render(request, 'category.html', context_dict)
これにより、サイトとその最新の説明のリストが表示されるので、category.html に次のように記述します。
{% if category %}
<h1>{{ category.name }}</h1>
{% for subcategory in category.subcategory_set.all %}
<a href="/links/{{ category.slug }}/{{ subcategory.slug }}">{{ subcategory.name }} ({{ subcategory.website_set.all|length }}) </a>
{% endfor %}
{% if descs %}
{% load endless %}
{% paginate descs %}
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo" %}
<ul id='list' class='linksteps'>
<a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">
<img src="/static/screenshots/{{ desc.about_id }}.png" />
</a>
<li><h3><a href="/{{ desc.about_id }}" rel="nofollow" target="_blank">{{ desc.about_id }}</a>{% if desc.title %} - {{ desc.title }} {% endif %}</h3>
{% if desc.description %}<b>Description: </b>{{ desc.description }}
<br />{% endif %} {% if desc.subject %}<b>Keywords: </b>{{ desc.subject }}
<br />{% endif %} {% if desc.type %}<b>Type: </b>{{ desc.type }}
<br />{% endif %} {% if desc.officialInfo %} {% if desc.language %}<b>Language: </b>{{ desc.language }}
<br />{% endif %} {% if desc.contactInformation %}<b>Contact info: </b>{{ desc.contactInformation }}
<br />{% endif %}
{% else %}
{% endif %}
</li>
</ul>
</div>
{% endfor %}
{% show_pages %}
{% else %}
<strong>No websites currently in category.</strong>
{% endif %}
{% else %}
The specified subcategory {{ category_name }} does not exist!
{% endif %}
最初はdictsortを使用しました
{% for desc in descs|dictsortreversed:"description"|dictsortreversed:"officialInfo"|dictsortreversed:"referral" %}
希望の順序でリストを提供してくれるので、私はすべて満足していました;)しかし、リストが長くなりすぎたため、ページネーションが必要であると判断しました。django-endless-pagination は正常に機能し、想定どおりに機能しますが、dictsort が開始される前にリストが分割されます。
ページネーションが発生する前と、最初のクエリでordered_byした後に並べ替えて、最新の説明を選択する方法はありますか?
とても感謝しております
編集:
答えが得られないので、私の質問は明確ではないかもしれません。
私が理解している限り、テンプレートのようにdictsortを置き換えて、views.pyの最後にcontext_dictの値をソートする必要があります
解決済み:::
これを行うことで、dictsort を置き換えることができました。
descs1 = sorted(descs, key=operator.attrgetter('referral', 'officialInfo', 'description'), reverse=True)
context_dict['descs'] = descs1