Problem using template tags
See original GitHub issueI 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:
- Created 8 years ago
- Comments:13 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 ablock
.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:
Posting this in the hopes that it might help others to avoid debugging the template tag for multiple hours.
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.