开发者

How to itemize/edit/delete results returned from a custom filter

Here is my filter:

@register.filter
def filter_by_user(queryset, user):
    """Filter the queryset by (currently logged in) user"""
    return queryset.filter(accountowner=user)

Here is my template:

<td>{{ object_list.all|filter_by_user:user|safeseq|join:". " }}</td>

Here is the output:

ACB test 5.0 55.0. ACB test 14.0 144.0 

Here is w开发者_开发百科hat I d rather have

1. ACB test 5.0 55.0 Edit Delete

2. ACB test 14.0 144.0 Edit Delete


Use unordered_list rather than join - despite the name, if you wrap it in <ol>...</ol> tags, it'll be an ordered list.

However, you should really be doing this in the view.

Edit

Actually, here's a better solution:

<ol>
{% for obj in object_list.all|filter_by_user:user %}
    <li>{{ obj }} <a href="{% url edit_object obj.id %}">Edit</a> <a href="{% url delete_object obj.id %}"Delete</a></li>
{% endfor %}
</ol>

Of course, this relies on you having edit_object and delete_object urls in your urlconf.


Well, then you would need to save the result in a variable, and iterate through it, building your links. Don't hardcode the links in the filter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜