3

I try to make my own assignment template tag in Django 1.6 (edit: I also tried it with 1.5.5 w/o success) but I can't get it to do anything.

In templatetags/getattribute.py I have:

from django import template
import logging

register = template.Library()
logger = logging.getLogger('wwwcid.website.debug_console')

#@register.assignment_tag
def mytag(context, context_variable):
    raise template.TemplateSyntaxError("buuuuuuuuuuuuuuus")
    logger.debug("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
    print "teeeeeeeeeeeeeeeeeeeest"
    return 'tpppppppppppppppppppppeee'
register.assignment_tag(mytag, takes_context=True)

In my template profile.html I have:

{% extends "website/base.html" %}
{% load getattribute %}
{% mytag "user.email" as vari %}
{{ vari }}
{% block content %}
<p> Rest of the page. </p>
{% endblock content %}

But this tag does not work. No error is raised (even if I raise it myself), no logging output, no print, no return, nothing.

Even if I register the tag w/o the takes_context=True, say just @register.assignment_tag it does not work. What am I doing wrong here? Thanks for your help!

frixx
  • 143
  • 8
  • I assume the typo in the above template snippet (closing `}%` rather than `%}`) is not actually present in your template, and that the `templatetags` directory is part of an app that's `settings.INSTALLED_APPS`? – Peter DeGlopper Nov 29 '13 at 20:36
  • Thanks for the typo (fixed it), which is not present in the actual code. Yes, I actually have a working filter in the same `getattribute.py` file.. the getattribute filter. – frixx Nov 29 '13 at 20:39
  • How can I get more debug output about what is happening? The problem is actually that it does silently nothing. – frixx Nov 29 '13 at 20:42
  • `settings.TEMPLATE_DEBUG` is probably the starting point, if you haven't already turned that on (and the `settings.DEBUG` setting that it requires). I might also try dropping an `import pdb; pdb.set_trace()` call into the tag code, then running the dev server - it should drop into the debugger if the tag code is ever executed. – Peter DeGlopper Nov 29 '13 at 20:45
  • settings.DEBUG = True and settings.TEMPLATE_DEBUG = DEBUG were already set. Thanks for th pdb hint but unfortunately this gets never executed. – frixx Nov 29 '13 at 20:52
  • Maybe drop into the debugger outside the definition to inspect `register` directly when the library gets loaded? – Peter DeGlopper Nov 29 '13 at 20:54
  • I stepped through `register.assignment_tag(...)` with pdb and as far as I can tell it runs through smoothly. I don't really see how I can test if it worked. – frixx Nov 29 '13 at 21:20

1 Answers1

7

OK, I should have pasted the code as I really have it in my app. I edited the profile.html part above to reflect the actual situation. The issue was that the tags and variables are only executed between {% block foo %} and {% endblock foo %}. So with the following profile.html template it works:

{% extends "website/base.html" %}
{% load getattribute %}
{% block content %}
{% mytag "user.email" as vari %}
{{ vari }}
<p> Rest of the page. </p>
{% endblock content %}
frixx
  • 143
  • 8