I have sorting django sortingtags component on my page.
I want render differently my page depending on what field it 开发者_开发百科is sorted by.
How can i implement that?
example:
{% autosort object_list %}
<tr>
<th>{% anchor first_name Name %}</th>
<th>{% anchor creation_date Creation %}</th>
...
</tr>
it become :
<tr>
<th><a href="/path/to/your/view/?sort=first_name" title="Name">Name</a></th>
<th><a href="/path/to/your/view/?sort=creation_date" title="Name">Creation</a></th>
...
</tr>
It renders me the same page depending on what link I cklicked but how can inspect what field it sorted by?
With Jinja2 templating engine you can have:
{% for tag in tags.order_by(sort_method) %}
<a href="{{tag.get_absolute_url()}}">{{tag.name}}</a>
{% endfor %}
I'm not sure if django templating will allow filter in the {% for %}
clause, but you can try creating a filter that would add the order_by
clause to the tags.
Otherwise a custom template tag for iteration over the tag objects may be a solution. I'm not using django templating engine any more.
I got it. As far as the link looks like /path/to/your/view/?sort=first_name
, so you just need in views function make get from request: sorted_by=request.GET.get('sort') than send it in to template. Thats it!
精彩评论