swappsco / django-qa

A simple Q&A App
http://django-qa.readthedocs.io/en/latest/index.html
MIT License
99 stars 34 forks source link

Non-ascii in Question or Answer causes UnicodeEncodeError #43

Closed yuriiz closed 6 years ago

yuriiz commented 6 years ago

Steps to reproduce:

  1. Enter non-ascii text in question or answer
  2. Visit /admin/qa/answer/pk/change/
  3. Get Error during template rendering In template /home/u/freedomregistry/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 21 Line 21: › {% if add %}{% blocktrans with name=opts.verbose_name %}Add {{ name }}{% endblocktrans %}{% else %}{{ original|truncatewords:"18" }}{% endif %}
    
    Traceback:  

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner

  1. response = get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _legacy_get_response

  1. response = self._get_response(request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response

  1. response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response

  1. response = response.render()

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py" in render

  1. self.content = self.rendered_content

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py" in rendered_content

  1. content = template.render(context, self._request)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/backends/django.py" in render

  1. return self.template.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/opbeat/instrumentation/packages/base.py" in call

  1. args, kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/opbeat/instrumentation/packages/base.py" in call_if_sampling

  1. return self.call(module, method, wrapped, instance, args, kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/opbeat/instrumentation/packages/django/template.py" in call

  1. return wrapped(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. return self._render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/api/function_trace.py" in dynamic_wrapper

  1. return wrapped(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in _render

  1. return self.nodelist.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render_annotated

  1. return self.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader_tags.py" in render

  1. return compiled_parent._render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/api/function_trace.py" in dynamic_wrapper

  1. return wrapped(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in _render

  1. return self.nodelist.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render_annotated

  1. return self.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader_tags.py" in render

  1. return compiled_parent._render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/api/function_trace.py" in dynamic_wrapper

  1. return wrapped(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in _render

  1. return self.nodelist.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render_annotated

  1. return self.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/defaulttags.py" in render

  1. return nodelist.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render_annotated

  1. return self.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/newrelic/hooks/framework_django.py" in wrapper

  1. return wrapped(*args, **kwargs)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader_tags.py" in render

  1. result = block.nodelist.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render_annotated

  1. return self.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/defaulttags.py" in render

  1. return nodelist.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render_annotated

  1. return self.render(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in render

  1. output = self.filter_expression.resolve(context)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py" in resolve

  1. new_obj = func(obj, *arg_vals)

File "/app/.heroku/python/lib/python2.7/site-packages/django/template/defaultfilters.py" in _dec

  1. args[0] = force_text(args[0])

File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/encoding.py" in force_text

  1. s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /admin/qa/answer/8/change/ Exception Value: 'ascii' codec can't encode character u'\u2019' in position 257: ordinal not in range(128)


The cause is `__str__`/`__unicode__` methods of models are being not properly implemented. It must be wrapped with `python_2_unicode_compatible` as described on
https://docs.djangoproject.com/en/1.11/ref/models/instances/#str