TemplateSyntaxError whenever I try to use a filter in my templates

testlist is just a list of objects. for instance

testlist.0.name 

- it's just "Test3"

I have a temp.html file

 {% extends 'base.html' %} {% block content %} {{testlist.0.name | safe}} {% endblock %} 

that everything in temp.html and base.html works fine with all other html files that use it

temp.html gives me

 TemplateSyntaxError at /mytests/ Could not parse the remainder: ' | safe' from 'testlist.0.name | safe' Request Method: GET Request URL: http://127.0.0.1:8000/mytests/ Django Version: 1.4 Exception Type: TemplateSyntaxError Exception Value: Could not parse the remainder: ' | safe' from 'testlist.0.name | safe' 

when i change it to:

 {% extends 'base.html' %} {% block content %} {{testlist.0.lastedited |date:"SHORT_DATE_FORMAT" }} {% endblock %} 

it gives me

 TemplateSyntaxError at /mytests/ could not parse some characters: testlist.0.lastedited| ||date:"SHORT_DATE_FORMAT" Request Method: GET Request URL: http://127.0.0.1:8000/mytests/ Django Version: 1.4 Exception Type: TemplateSyntaxError Exception Value: Could not parse some characters: testlist.0.lastedited| ||date:"SHORT_DATE_FORMAT" 

you get the idea. It seems like I just can't use filters in my django templates. I tried other filters and still got the same thing. Am I missing some parameters that allow me to use the channel symbol? Could it be that "|" The key on my macbook pro is not a channel character, but some other character that django does not recognize?

+6
source share
1 answer

It looks like you need to remove the spaces between testlist.0.lastedited and the filter. Try something like:

 {% extends 'base.html' %} {% block content %} {{testlist.0.name|safe}} {% endblock %} 

I suppose this is your problem, because they have no spaces in the documents, and they parse the template as a string or something close to it, so spaces can affect it.

Template example

+9
source

Source: https://habr.com/ru/post/919435/


All Articles