question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem using template tags

See original GitHub issue

I am having difficulty using template tags in my templates. For example this works

In the view:

from __future__                 import absolute_import
import rules
...
def detail(request, slug):
    obj = get_object_or_404(NewsStory, slug=slug)
    return render(request, 'news/detail.html', {
        'story': obj,
        'can_publish_newsstory': rules.has_perm('can_publish_newsstory', request.user)
        })

In the template

       {% if can_publish_newsstory %}
        <li><a href="{% url 'news:edit' story.slug %}" class="button expand secondary">Edit Story</a></li>
        <li><a href="{% url 'news:delete' story.slug %}" class="button expand secondary">Delete Story</a></li>
        {% endif %}

If I change this to remove ‘can_publish_newsstory’ from the view and include it in the template, nothing shows.

Revised Template

{% load rules %}
{% has_perm 'can_publish_newsstory' user as can_publish_newsstory %}
       {% if can_publish_newsstory %}
        <li><a href="{% url 'news:edit' story.slug %}" class="button expand secondary">Edit Story</a></li>
        <li><a href="{% url 'news:delete' story.slug %}" class="button expand secondary">Delete Story</a></li>
        {% endif %}

I am using Python 2.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
cfracommented, Mar 29, 2020

I was pulling out my hair over a problem which appears very similar to the one described here.

In my case, the problem was caused by using the has_perm template tag outside of a block.

While this parses without any warnings, the tag will never be called and its return value is not available in the scope where it is needed.

I was able to resolve it like this:

--- a/apps/example/templates/example/thing_frobnicate.html
+++ b/apps/example/templates/example/thing_frobnicate.html
@@ -6,11 +6,10 @@
 {% load thumbnail %}
 {% load wagtailcore_tags %}
 
-{% has_perm 'example.frobnicate_thing' user thing as frobnication_is_authorized %}
-
 {% block body_class %}frobnication-info{% endblock %}
 
 {% block content %}
+   {% has_perm 'example.frobnicate_thing' user thing as frobnication_is_authorized %}
    <div class="pageheader">
        {% if frobnication_is_authorized %}

Posting this in the hopes that it might help others to avoid debugging the template tag for multiple hours.

0reactions
dfuncktcommented, Jun 3, 2015

I’m closing the issue for now. If you get a test project together or you find it’s even remotely related to rules please feel free to reopen and post back.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem loading custom template tags (Error: No module ...
Use manage.py shell then from myapp.templatetags import myapp_tags to find out if theres any python error in the myapp_tags.py file.
Read more >
Built-in template tags and filters - Django documentation
This document describes Django's built-in template tags and filters. It is recommended that you use the automatic documentation, if available, ...
Read more >
Template Tag Not Working (Example) | Treehouse Community
Template Tag Not Working. Please find the code below I am getting an error. code_challenges/templatetags/fairy_extras.py. from django import ...
Read more >
if - Django Template Tags - GeeksforGeeks
This article revolves about how to use if tag in Templates. The {% if %} tag evaluates a variable, and if that variable...
Read more >
Problem with templatetags and template inheritance
templates and template tags. ... a tag {% get_conten_sectiont %} is used. ... just creates a ContentNode which has an empty constructor. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found