Akshay Pratap Singh
Akshay Pratap Singh

Reputation: 3327

Can't pass object of model in arguments of django custom template filter

I created a custom filter in django template, but django has some constraints in passing no. of arguments, it allows only one or two arguments for custom filter functions Read more. And, i want to pass two arguments to my custom filter one is string and second one is object of model class. But can't able to successfully accomplish this.

index.html

{% load has_permission_filter %}
{% for u in users %}
 <span class={{ user|has_location_perm:('user.view_user', u)}}>View</span>
{% endfor %}

In above template user is current logged user and users is a list of instances of User model class.

has_permission_filter.py

def has_location_perm(user, args):
    perm_str, obj = args[0], args[1]
    // business logic
    if user.has_perm(perm_str) and business_logic_check(perm_str, obj):
        return 'allow'
    else:
        return 'not-allow'

So, I want a some sort of solution which helps me in passing object in filter function from django template.

Upvotes: 0

Views: 2056

Answers (1)

Sander van Leeuwen
Sander van Leeuwen

Reputation: 3033

You can't use multiple arguments with filters. You can with a simple tag though.

Upvotes: 4

Related Questions