0

Here's what I'm trying to achieve in "pseudo code":

{% for page in pages %}
    {% if 'can_access_page_{{page.name}}' in perms  %}
        <li>
            <a href="{{page.url}}" id="page_link_{{page.id}}" target="_blank">{{ page.name }}</a>
        </li>
    {% endif %}
{% endfor %}

How to do this? Permission names I can customize — but still can't figure out this one.

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
Capuchin
  • 3,465
  • 6
  • 28
  • 40

2 Answers2

1

You'll need a custom filter. Something like:

@register.filter
def check_page_perms(page, perms):
    return 'can_access_page_%s' % page.name in perms

and use it:

{% if page|check_page_perms:perms %}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

Simplest way is to slightly abuse Django's existing add template filter (intended for numbers but works for strings), as in this answer:

https://stackoverflow.com/a/4524851/202168

Community
  • 1
  • 1
Anentropic
  • 32,188
  • 12
  • 99
  • 147