0

I have a blog site with a form for creating and updating posts. For certain content, when I copy/paste from a word document into the form for creating/editing I get a key error. It doesn't happen for text that is directly written into the form and it doesn't always happen for text that is copied from Microsoft word.

I am completely flummoxed about this. Any help would be much appreciated.

views.py:

def post_detail(request, slug=None):
    instance = get_object_or_404(Post, slug=slug)
    if instance.publish > timezone.now().date() or instance.draft:
        if not request.user.is_staff or not request.user.is_superuser:
            raise Http404
    share_string = quote_plus(instance.content)

    ...

Error message:

Traceback:

File "/Users/cward/projects/swanson_speech_therapy2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/Users/cward/projects/swanson_speech_therapy2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/cward/projects/swanson_speech_therapy2/blog/src/posts/views.py" in post_detail
  49.   share_string = quote_plus(instance.content)

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py" in quote_plus
  1308.         s = quote(s, safe + ' ')

File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py" in quote
  1303.     return ''.join(map(quoter, s))

Exception Type: KeyError at /blog/test-8-9-10-11/
Exception Value: u'\u2019'
xyres
  • 20,487
  • 3
  • 56
  • 85
Chris
  • 363
  • 5
  • 20
  • 2
    before saving, make sure you decode unicode chars. I think the problem is there. – Lemayzeur Dec 15 '17 at 21:47
  • 1
    @Lemayzeur is right, except you have to *encode* the content as utf8, not decode. See this: https://stackoverflow.com/questions/15115588/urllib-quote-throws-keyerror – xyres Dec 15 '17 at 21:49
  • 1
    the accepted answer from the link given by @xyres will solve it, but some chars may be lost. know more about how django stores characters in the database https://docs.djangoproject.com/en/2.0/ref/unicode/ – Lemayzeur Dec 15 '17 at 21:58

0 Answers0