Gr1N
Gr1N

Reputation: 1377

assignment_tag TemplateSyntaxError

I try to use assignment_tag from django docs: https://docs.djangoproject.com/en/1.4/howto/custom-template-tags/#howto-custom-template-tags-simple-tags

Test project:

mysite/
manage.py
polls/
    views.py
    ...
mysite/
    ...
templates/
    polls/
        detail.html

In polls/views.py:

from django.template import Library

register = Library()

@register.assignment_tag
def get_text():
    return 'TEST TEXT'

Then I add code to templates/polls/detail.html

{% get_text as text %}
<p>The text is {{ text }}.</p>

But this is not work, I understand that get_text not visible, but I do not know how to do it right.

Upvotes: 1

Views: 210

Answers (1)

Alasdair
Alasdair

Reputation: 309089

Your template tag should not go in the views.py file. You need to create a module in your app's templatetags directory. Have a look at the code layout docs for template tags.

Secondly, remember to load your tag in the template before you use it, with the {% load %} tag.

If you still have problems, update your question and include the full traceback -- 'template syntax error' isn't enough information for us to work out what's going on.

Upvotes: 1

Related Questions