0

I have a python script which is used to create static html files using Djnago templates. it looks like follows:

....
from django.template import Template, Context
from django.conf import settings
settings.configure()
template = """
   <!DOCTYPE html>
            <html lang="en">
             ..........................
             ..........................
   </html>
   """

t = Template(template)
c = Context({"field1":field1_from_program,
             "field2": some_dictionary,
              ......
              ......
              "field1000": some_other_list
            })

    f1=open(args.name+'.html', 'w+')
    try:
        f1.write(t.render(c).encode('utf-8'))
    except UnicodeEncodeError:
        f1.write(t.render(c).encode('ascii', 'ignore').decode('ascii'))
    f1.close()

In some occasions, I get the following error

Traceback (most recent call last):
  File "script.py", line 733, in <module>
    main()
  File "script.py", line 723, in main
    f1.write(t.render(c).encode('ascii', 'ignore').decode('ascii'))
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 209, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 201, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 903, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 917, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 957, in render
    output = self.filter_expression.resolve(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 647, in resolve
    obj = self.var.resolve(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 787, in resolve
    value = self._resolve_lookup(context)
  File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 847, in _resolve_lookup
    current = current()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 3: ordinal not in range(128)

I found some fields in the content were causing this error, so I added .encode('ascii', 'ignore').decode('ascii') to the variable which was causing this error. However, it is not obvious from the error log. There are many more such values, and the error message is not showing a verbose output on which field is causing this error. In other words, if "field100":value_for_field100,, I would want to know that it is the problem with value_for_field100 may be using a try catch block.

In that sense, please note that my question is different than other SO questions such as this or this. Hence, I genuinely believe this is not a duplicate question.

Any tips would be much appreciated.

kingmakerking
  • 2,017
  • 2
  • 28
  • 44
  • 1
    The result returned by t.render(c) is unicode, how about f1.write(t.render(c).encode('utf-8')) ? – Gabriel Muj Apr 13 '17 at 13:14
  • @MujGabriel Well, that might fix the problem, but I want to know which field is causing the problem instead. Thanks anyways. – kingmakerking Apr 13 '17 at 14:09
  • If you want to debug this you can try iterating over the dictionary (from Context) and apply str function on the value (do this inside a try/except and print the key and value) before doing any rendering. – Gabriel Muj Apr 13 '17 at 16:25

0 Answers0