Created
July 8, 2012 16:19
-
-
Save artofhuman/3071604 to your computer and use it in GitHub Desktop.
Django: Sort inclusion tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@register.inclusion_tag('catalog/sort.html', takes_context = True) | |
def catalog_sort(context, detail_url): | |
sort = [ | |
{'name': 'Популярности', 'sort': 'sort'}, | |
{'name': 'Цене', 'sort': 'price', 'active': False}, | |
{'name': 'Названию', 'sort': 'name', 'active': False} | |
] | |
if context['request'].GET.get('sort'): | |
for sort_item in sort: | |
if context['request'].GET.get('sort') == sort_item.get('sort'): | |
sort_item['active'] = True | |
sort_item['sort'] = '-' + sort_item.get('sort') | |
sort_item['direction'] = 'asc' | |
elif context['request'].GET.get('sort') == '-' + sort_item.get('sort'): | |
sort_item['active'] = True | |
sort_item['direction'] = 'desc' | |
return {'sort': sort} | |
========================================================== | |
{% catalog_sort category.get_absolute_url %} | |
========================================================== | |
Template | |
<ul class="b-sort b-catalog-items__sort"> | |
<li class="b-sort__title">Сортировать по:</li> | |
{% for sort_item in sort %} | |
{% if sort_item.active %} | |
<li class="b-sort__item b-sort__item_state_current"> | |
<a class="b-link" href="{{ detail_url }}?sort={{ sort_item.sort }}"> | |
<span class="b-link__inner">{{ sort_item.name }}</span> | |
<img class="b-icon b-icon_action_sort-{{ sort_item.direction }}" src="/static/images/blank.gif" alt=""/> | |
</a> | |
</li> | |
{% else %} | |
<li class="b-sort__item"> | |
<a class="b-link" href="{{ detail_url }}?sort={{ sort_item.sort }}">{{ sort_item.name }}</a> | |
</li> | |
{% endif %} | |
{% endfor%} | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment