AlexW
AlexW

Reputation: 2587

Django - context process query being repeated 102 times

I have built a context process that sends the logged on users permissions as a string into a template. Then based on that users perms I show or hide urls.

However using the debug toolbar ive just seen that the query is run 102 times for a reason I do not know

The debug (the ID changes and it looks as if there are 3 of each ID in the duplicates)

SELECT `django_content_type`.`id`, `django_content_type`.`app_label`, `django_content_type`.`model` FROM `django_content_type` WHERE `django_content_type`.`id` = 35
  Duplicated 102 times. 
0.6862959744274587%
23.41   
Sel Expl
Connection: default
/itapp/itapp/sites/views.py in site_detail_files(214)
  'PageType' : 'files',
/usr/local/lib/python3.6/contextlib.py in __enter__(81)
  return next(self.gen)
/itapp/itapp/itapp/context_processors.py in UserPerms(34)
  'Perms': str(all_perms),

the function:

def UserPerms(request):
    from django.contrib.auth.models import Permission
    all_perms = []
    if str(request.user) != 'AnonymousUser':
        permissions = Permission.objects.filter(user=request.user)
        group_permissions = Permission.objects.filter(group__user=request.user)

        all_perms = []
        for p in permissions:
            all_perms.append(p)
        for p in group_permissions:
            all_perms.append(p)
    return {
        'Perms': str(all_perms),
    }  

added to templates in settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR + '/templates/',
            ],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug' : DEBUG,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'itapp.context_processors.breadcrumb_history',
                'itapp.context_processors.UserPerms',
            ],
        },
    },
]

usage example:

<li><a href="{% url 'sites:site_detail_circuits' SiteID %}">Circuits</a>
{% if "Permission: sites | Circuit Data | Can add Circuit Data" in Perms %}     
    {% if PageType == 'circuits' %}
    <ul>
        <li><a href="{% url 'admin:sites_circuits_add' %}?site_data={{ SiteID }}">Add new circuit</a></li>

    </ul>
    {% endif %}
{% endif %}
</li> 

Upvotes: 0

Views: 156

Answers (1)

Alasdair
Alasdair

Reputation: 309039

Django provides a way to check permissions in the template. You don't have to create a string containing the permissions.

{% if perms.sites.add_circuit_data %}
{% if PageType == 'circuits' %}
<ul>
    <li><a href="{% url 'admin:sites.add_circuit' %}?site_data={{ SiteID }}">Add new circuit</a></li>

</ul>
{% endif %}

You need the auth context processor enabled for this to work, but since this is in the default generated settings file you shouldn't have to change anything.

Upvotes: 1

Related Questions