2

I have a Google app engine in python form submit that POSTS text to a server, and the text gets encoded with the encoding Quoted Printables. My code for POSTing is this:

<form action={{ upload_url }} method="post" enctype="multipart/form-data">
<div class="sigle-form"><textarea name="body" rows="5"></textarea></div>
<div class="sigle-form"><input name="file" type="file" /></div>
</form>

Then the result of the fetching self.request.get('body') will be encoded with the encoding Quoted Printables. I store this in text DB.textProperty() and later sends the text to a HTML template using Django. When i write out the variable using {{ body }}, the result is written with Quoted printable encoding, and it does not seem that there is a way of decoding this in the Django HTML template. Is there any way of encoding the text in the body thats sent on another way than with Quoted Printables? If not, how to decode this encoding in the Django HTML template?

The result for submiting the text "ÅØÆ" is encoded to " xdjG ", so the sum of the Quoted Prinables are somehow added togheter as well. This happens when more than one special character are present in the encoded text. An ordinary "ø" is encoded to =F8.

EDIT: I get this problem only in production, and this thread seems to talk about the same problem.

If anyone else here on Stack Overflow are doing form submit with blobs and åæøè characters, please respond to this thread on how you have solved it!

bogen
  • 9,954
  • 9
  • 50
  • 89

3 Answers3

1

Ok, after two days working with this issue i finally resolved it. Its seemingly a bug with Google App Engine that makes the encoding f'ed up in production. When in production the text is sometimes encoded with Quoted Printable encoded, and some other times encoded with base64 encoding. Weird. Here is my solution:

postBody = self.request.get('body')    
postBody = postBody.encode('iso-8859-1')
DEBUG = os.environ['SERVER_SOFTWARE'].startswith('Dev')
if DEBUG:
    r.body = postBody
else:
    postBody += "=" * ((4 - len(postBody) % 4) % 4)
    b64 = base64.urlsafe_b64decode(postBody)

Though the resulting b64 can't be stored in the data storage because it's not ascii encoded

'ascii' codec can't decode byte 0xe5 in position 5: ordinal not in range(128)
bogen
  • 9,954
  • 9
  • 50
  • 89
1

I solved a similar problem by using the Python quopri module to decode the string before passing it to an HTML template.

import quopri
body = quopri.decodestring(body)

This seems to be something to do with the multipart/form-data enctype. Quotable printable encoding is applied to the textarea input, which is then, in my case, submitted via a blobstore upload link. The blobstore returns the text to my upload handler still in encoded form.

  • Exactly what I needed to get a URL out of a `Content-Transfer-Encoding: quoted-printable` formatted e-mail body. [quopri](https://docs.python.org/3/library/quopri.html) even is in the Standard Library - and I thought I had seen them all by now. – ojdo Dec 06 '19 at 12:57
0

Not sure what Quoted Printables are but have you tried safe?

{{ body|safe }}

https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#safe

Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36
  • Yes. It does not seem to correctly decode Quoted Printable encoding. – bogen Oct 23 '12 at 07:01
  • It's strange then as you would not expect any conversion to happen at all at any stage. Have you tried not storing it and just writing it straight back out to determine if the storage phase is reformatting the data somehow? – Paul Collingwood Oct 23 '12 at 08:04
  • Yes, i do this in the receiving method: `postBody = self.request.get('body') self.response.out.write(postBody)` Allready then it has encoded the text to Quoted Printable encoding – bogen Oct 23 '12 at 08:06
  • Is your HTML property configured, with an encoding set etc? – Paul Collingwood Oct 23 '12 at 08:16
  • The meta tag with encoding in the submit-html is – bogen Oct 23 '12 at 14:24
  • The strange thing is that Æ Æ shows right when submitting on Browsing local version, its when i deploy it shows as "xiDG". – bogen Oct 23 '12 at 14:37
  • what about the encoding in the display html? – Paul Collingwood Oct 23 '12 at 15:06
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18506/discussion-between-hakonbogen-and-paul-c) – bogen Oct 24 '12 at 07:01