title: UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
body: <p>I see tons of questions that are related to this but I can't figure out the solution.</p>
  
  <p>This is on Django 1.4 and Python 2.7.</p>
  
  <p><code>data</code> is a dictionary that contains UTF8 characters. See this line: </p>
  
  <p><code>render_to_response('application/app.html', data, context_instance=RequestContext(request))</code> </p>
  
  <p>Template gets rendered that outputs values from that <code>data</code>.</p>
  
  <p>Why does it explode and what can I do to fix this?</p>
  
  <p><strong>EDIT</strong>: After digging around, part of that <code>data</code> contains <code>lxml.objectify.ObjectifiedElement</code>. Basically a XML element that can be queried like a normal dictionary. Values that it produces appear to be proper unicode strings like this: <code>u'\xae\u2020\xa5\xa8\u02c6\xf8'</code></p>
  
  <p>Here's the full stacktrace:</p>
  
  <pre><code>File "/web/mysite/env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  
  File "/web/mysite/current/api/views.py", line 163, in invoice
    return render_to_response('application/app.html', data, context_instance=RequestContext(request))
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 20, in render_to_response
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/loader.py", line 176, in render_to_string
    return t.render(context_instance)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    return compiled_parent._render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/defaulttags.py", line 281, in render
    return nodelist.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 837, in render_node
    return node.render(context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 880, in render
    return _render_value_in_context(output, context)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/template/base.py", line 858, in _render_value_in_context
    value = force_unicode(value)
  
  File "/web/mysite/env/lib/python2.7/site-packages/django/utils/encoding.py", line 74, in force_unicode
    s = unicode(str(s), encoding, errors)
  
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-4: ordinal not in range(128)
  </code></pre>