[Django]-Syntax error whenever I put Python code inside a Django template

3šŸ‘

āœ…

Instead of using a slice assignment to grow a list

embed_list[len(embed_list):] = [foo]

you should probably just do

embed_list.append(foo)

But really you should try unescaping html with a library function rather than doing it yourself.

That NoneType error sounds like embed.replace is None at some point, which only makes sense if your list is not a list of strings ā€“ you might want to double-check that with some asserts or something similar.

šŸ‘¤lacker

8šŸ‘

I am quite sure that Django templates does not support that.
For your replace operation I would look into different filters.

You really should try to keep as much logic as you can in your views and not in the templates.

7šŸ‘

Djangoā€™s template language is deliberately hobbled. When used by non-programming designers, this is definitely a Good Thing, but there are times when you need to do a little programming. (No, I donā€™t want to argue about that. This has come up several times on django-users and django-dev.)

Two ways to accomplish what you were trying:

  • Use a different template engine. See Jinja2 for a good example that is fully explained for integrating with Django.
  • Use a template tag that permits you to do Python expressions. See limodouā€™s Expr tag.

I have used the expr tag in several places and it has made life much easier. My next major Django site will use jinja2.

šŸ‘¤Peter Rowell

4šŸ‘

I donā€™t see why youā€™d get ā€œNoneType object is not callableā€. That should mean that somewhere on the line is an expression like ā€œfoo(ā€¦)ā€, and it means foo is None.

BTW: You are trying to extend the embed_list, and itā€™s easier to do it like this:

embed_list = []
for embed in embeds:
    embed_list.append(embed.replace("&lt;", "<")) #this is line 35
return render_to_response("scanvideos.html", {"embed_list":embed_list})

and even easier to use a list comprehension:

embed_list = [embed.replace("&lt;", "<") for embed in embeds]

3šŸ‘

Django templates use their own syntax, not like Kid or Genshi.

You have to roll your own Custom Template Tag.

I guess the main reason is enforcing good practice. In my case, Iā€™ve already a hard time explaining those special templates tags to the designer on our team. If it was plain Python Iā€™m pretty sure we wouldnā€™t have chosen Django at all. I think thereā€™s also a performance issue, Django templates benchmarks are fast, while last time I checked genshi was much slower. I donā€™t know if itā€™s due to freely embedded Python, though.

You either need to review your approach and write your own custom templates (more or less synonyms to ā€œhelpersā€ in Ruby on Rails), or try another template engine.

For your edit, thereā€™s a better syntax in Python:

embed_list.append(embed.replace("&lt;", "<"))

I donā€™t know if itā€™ll fix your error, but at least itā€™s less JavaScriptesque šŸ˜‰

Edit 2: Django automatically escapes all variables. You can enforce raw HTML with |safe filter : {{embed|safe}}.

Youā€™d better take some time reading the documentation, which is really great and useful.

šŸ‘¤vincent

Leave a comment