Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateSyntaxError whenever I try to use a filter in my templates

testlist is just a list of objects. For example

testlist.0.name 

is simply "Test3"

I have a file temp.html

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

that's all there is in the temp.html file 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 that I just cannot use any filters in my django templates. I tried other filters and still get the same thing. Am I missing some options that enable the use of the pipe character? Can it be that the "|" key on my macbook pro is not the pipe character but some other character that django fails to recognise?

like image 699
panosmm Avatar asked Jul 01 '12 16:07

panosmm


1 Answers

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

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

I'm guessing this is your problem, since in the docs, they don't have any whitespace, and they parse the template as a string or something close to it, so whitespace can affect it.

Template example

like image 54
notbad.jpeg Avatar answered Oct 13 '22 07:10

notbad.jpeg