开发者

Making Django ignore string literals

UPDATE 2: The problem isn't a Django problem at all - Python was definitely stripping out the characters befor开发者_高级运维e it got to Django. The fix is to define the string as raw

inputs['variable'] = r'{\bf this is code} \\'

UPDATE: It turns out this is a deeper question than I thought at first glance - the issue is that python is replacing the string literals before they ever get to django. I will do more investigating and update this if I find a solution.

I'm using django to work with LaTeX templates for report generation, and am running into a lot of problems with the way Django replaces parts of strings.

Specficially, I've run into two problems where I try to insert a variable containing latex code.

The first was that it would replace HTML characters, such as the less than symbol, with their HTML codes, which are of course gibberish to a LaTeX interpreter. I fixed this by setting the context to never autoescape, like so:

c = Context(inputs)
c.autoescape = False

However, I still have my second issue, which is that Django replaces string literals with their corresponding characers, so a double backslash becomes \, and \b becomes a backspace. How can I force Django to leave these characters in place, so

inputs['variable'] = '{\bf this is code} \\'

won't get mangled when I use

{{variable}}

to reference it in the django template?


As noted in the updates above, it wasn't a problem with Django at all - Python was interpreting the string literals before the string even made it to Django. The fix was to define the string as raw.

inputs['variable'] = r'{\bf this is code} \\'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜