TheOne
TheOne

Reputation: 11159

How to add a method to all django templates?

What is the best way to add a method-like functionality to all templates in my project?

The method that I need is the following:

def has_access(user, code):
    return user has access to code

Within a template it woule be used like so:

{% if has_access(request.user, 'add-project') %}
    html code here
{% endif %}

Custom filters won't work since I am not "filtering" anything. Also, from what I can see, custom context processors don't accept extra parameters.

Not sure how to go about doing this in django.

Upvotes: 0

Views: 79

Answers (1)

Wolph
Wolph

Reputation: 80061

Custom filters actually would work:

{{ user|has_access:code }}

A custom template tag would work:

{% has_access user code %}

A middleware could work... but it is hard to say what's the best method for your problem without having more info.

Upvotes: 2

Related Questions