Question

I have a question on template render in GAE. I would like to import a text file and write it to a html file. In this text file, I have used html symbols to tag new lines new paragraphs. It worked well with webapp. However, the same expression does not work with webapp2. So please give me some suggestions.

Thank you!

    import webapp2        
    text_file2 = open('text1.txt','r')
    x = text_file2.read()  
    html = html + template.render(templatepath + 'A.html', {'model_attributes':'Overview','text_paragraph':x})
Was it helpful?

Solution

I don't believe that the problem is webapp2 as much as it is Django 1.2, which is, I believe, the default version that you get with GAE as of a recent release of the SDK. The Django 1.2 templating engine differs from the 0.96 version in that it automatically HTML-escapes the content of template variables, and that is probably changing what you expect to see in your render page.

To fix it, you should add the |safe filter to the variable substitution in your template. So, if your template has something like this:

{{ text_paragraph }}

it should look like this:

{{ text_paragraph|safe }}

You can find out more information about this, including more options for dealing with Django's HTML-escaping here

And here is the official Django documentation on the safe filter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top