python - How to get utf-8 from forms in Bottle? -


i trying use bottle.py input information users in web page.

everything works fine except when have latin characters (accents mostly). have try using utf-8 , latin-1 coding on first 2 lines of code, won't work.

#!/usr/bin/env python # -*- coding: utf-8 -*- import bottle  @bottle.post('/newpost') def post_newpost():     subject = bottle.request.forms.get("subject")     body = bottle.request.forms.get("body")     tags = bottle.request.forms.get("tags") 

and html code page is:

<html> <head> <meta charset="utf-8" /> <title>new posts</title> </head> <body> <form action="/newpost" method="post">  <h2>post title</h2> <input type="text" name="subject" size="120" value="{{subject}}" ><br> <h2>post<h2> <textarea name="body" cols="120" rows="20">{{body}}</textarea><br> <h2>tags</h2> <input type="text" name="tags" size="120" value="{{tags}}"><br> <p> <input type="submit" value="submit"> </body> </html> 

i read in bottle page that:

  • in python 3 strings unicode, http byte-based wire protocol. server has decode byte strings somehow before passed application. on safe side, wsgi suggests iso-8859-1 (aka latin1), reversible single-byte codec can re-encoded different encoding later. bottle formsdict.getunicode() , attribute access, not dict-access methods. these return unchanged values provided server implementation, not want.

    request.query['city']

    'göttingen' # utf8 string provisionally decoded iso-8859-1 server

    request.query.city

    'göttingen' # same string correctly re-encoded utf8 bottle

if need whole dictionary correctly decoded values (e.g. wtforms), can call formsdict.decode() re-encoded copy.

after reading tried using function don't know how. right bottle form returns strings, can not use encode('utf-8') or decode('utf-8').

please me!

thanks!

#!/usr/bin/env python # -*- coding: utf-8 -*- import bottle  @bottle.post('/newpost') def post_newpost():     subject = bottle.request.forms.subject     body = bottle.request.forms.body     tags = bottle.request.forms.tags 

that it.... thanks!


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -