[Django] #30705: Django template permissions not working when used inside the template of an inclusion tag

8 views
Skip to first unread message

Django

unread,
Aug 13, 2019, 5:37:35 AM8/13/19
to django-...@googlegroups.com
#30705: Django template permissions not working when used inside the template of an
inclusion tag
-------------------------------------+-------------------------------------
Reporter: erev0s | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Template | Version: 2.2
system | Keywords: permissions,
Severity: Normal | inclusion tag
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30705>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 16, 2019, 6:51:09 AM8/16/19
to django-...@googlegroups.com
#30705: Django template permissions not working when used inside the template of an
inclusion tag
-------------------------------------+-------------------------------------
Reporter: erev0s | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: 2.2
Severity: Normal | Resolution: needsinfo
Keywords: permissions, | Triage Stage:
inclusion tag | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => needsinfo


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30705#comment:1>

Django

unread,
Aug 30, 2019, 8:33:54 AM8/30/19
to django-...@googlegroups.com
#30705: Django template permissions not working when used inside the template of an
inclusion tag
-------------------------------------+-------------------------------------
Reporter: erev0s | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: 2.2
Severity: Normal | Resolution: needsinfo
Keywords: permissions, | Triage Stage:
inclusion tag | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by erev0s):

* Attachment "django-locallibrary-tutorial.tar.gz" added.

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

Django

unread,
Aug 30, 2019, 8:41:11 AM8/30/19
to django-...@googlegroups.com
#30705: Django template permissions not working when used inside the template of an
inclusion tag
-------------------------------------+-------------------------------------
Reporter: erev0s | Owner: nobody
Type: Uncategorized | Status: closed
Component: Template system | Version: 2.2
Severity: Normal | Resolution: needsinfo
Keywords: permissions, | Triage Stage:
inclusion tag | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (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.

[[Image(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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30705#comment:2>

Django

unread,
Aug 30, 2019, 11:33:35 AM8/30/19
to django-...@googlegroups.com
#30705: Django template permissions not working when used inside the template of an
inclusion tag
-------------------------------------+-------------------------------------
Reporter: erev0s | Owner: nobody
Type: Bug | Status: new

Component: Template system | Version: 2.2
Severity: Normal | Resolution:
Keywords: permissions, | Triage Stage:
inclusion tag | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Claude Paroz):

* status: closed => new
* resolution: needsinfo =>
* type: Uncategorized => Bug


Comment:

Reopening as per new provided material.

--
Ticket URL: <https://code.djangoproject.com/ticket/30705#comment:3>

Django

unread,
Sep 2, 2019, 3:50:26 AM9/2/19
to django-...@googlegroups.com
#30705: Django template permissions not working when used inside the template of an
inclusion tag
-------------------------------------+-------------------------------------
Reporter: erev0s | Owner: nobody
Type: Bug | Status: closed

Component: Template system | Version: 2.2
Severity: Normal | Resolution: invalid

Keywords: permissions, | Triage Stage:
inclusion tag | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* status: new => closed

* resolution: => invalid


Comment:

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 [https://docs.djangoproject.com/en/2.2/howto/custom-
template-tags/#inclusion-tags 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 [https://django-debug-
toolbar.readthedocs.io/en/latest/index.html Django Debug Toolbar] to see
exactly what templates are rendered, with exactly what contexts.

Please see TicketClosingReasons/UseSupportChannels for appropriate support
channels.

--
Ticket URL: <https://code.djangoproject.com/ticket/30705#comment:4>

Reply all
Reply to author
Forward
0 new messages