Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom assignment_tag does not get executed

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!

like image 354
frixx Avatar asked Jul 20 '26 01:07

frixx


1 Answers

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 %}
like image 76
frixx Avatar answered Jul 22 '26 13:07

frixx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!