#30705 closed Bug (invalid)

Django template permissions not working when used inside the template of an inclusion tag

Reported by: erev0s Owned by: nobody
Component: Template system Version: 2.2
Severity: Normal Keywords: permissions, inclusion tag
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have 2 apps in a Django Project i am using to learn django. In the app A I have an inclusion tag where I am using a template named templateA.html. This inclusion tag is being used in the app B, inside the template templateB.html. The content of templateA.html is shown properly inside templateB.html.

I noticed though that when I am attempting to check permissions of the user using code similar to the following:

{% if perms.appA.can_do_something %}
show something here
{% endif %}

then, if I include that code in templateA.html it is not working, while if I use it in templateB.html it is working properly. I am mentioning again that the templateA.html is the one used in the inclusion tag I am using in the templateB.html.

Attachments (1)

django-locallibrary-tutorial.tar.gz (63.4 KB) - added by erev0s 13 months ago.
Modifies this project https://github.com/mdn/django-locallibrary-tutorial to show the issue mentioned in the ticket

Download all attachments as: .zip

Change History (5)

comment:1 Changed 13 months ago by Carlton Gibson

Resolution: needsinfo
Status: newclosed

Hi. I suspect there's not a bug here but, as it stands there not enough information to know...

Could you upload a minimal project that recreates your issue so we can have a look? Thanks.

Changed 13 months ago by erev0s

Modifies this project https://github.com/mdn/django-locallibrary-tutorial to show the issue mentioned in the ticket

comment:2 Changed 13 months ago by erev0s

I have added as an attachment a modified version of the famous django example shown here https://github.com/mdn/django-locallibrary-tutorial
In the attachment you will find an additional app named "check_permissions". Inside the "templatetags" you will find the "custom_tag.py" where there is an inclusion tag using the "permision_required" decorator.

The template used for the inclusion tag is located in "catalog/templates/link-tag.html" and the custom tag is loaded in the "catalog/templates/index.html" which is the main page you find after you run the project.

After you have created an admin user and you setup and run the project you will see in the "http://localhost:8000/catalog/" at the bottom that you are able to see only the second "<li>" found there.

https://i.imgur.com/6Z6Nq6L.png

The permission inside the "link-tag.html" is NOT found probably or something similar, due to even admin can not access the content.

comment:3 Changed 13 months ago by Claude Paroz

Resolution: needsinfo
Status: closednew
Type: UncategorizedBug

Reopening as per new provided material.

comment:4 Changed 13 months ago by Carlton Gibson

Resolution: invalid
Status: newclosed

The block is not rendered because perms is not present in the context of your inclusion tag, so the conditional correctly evaluates to False.

Take a look at the Inclusion tags docs.
You'll need to use the takes_context kwarg and pass a reference to perms as per the examples there.

I recommend investigating Django Debug Toolbar to see exactly what templates are rendered, with exactly what contexts.

Please see TicketClosingReasons/UseSupportChannels for appropriate support channels.

Note: See TracTickets for help on using tickets.
Back to Top