Bottle - WTForms Problem
Verfasst: Mittwoch 10. August 2011, 18:58
Hallo,
ich habe ein Problem in der Kombi Bottle - WTForms, und zwar ein doofes Unicode-Problem.
Code:
und das Template:
Start man das Prog, wirft Bottle einen Fehler:
Ändert man "choices_list" in
dann wirft WTForms den Fehler:
Und ich habe so gar keine Idee, wie ich das Lösen kann bzw. was ich machen muss, damit das funktioniert...
Gruß, noisefloor
ich habe ein Problem in der Kombi Bottle - WTForms, und zwar ein doofes Unicode-Problem.
Code:
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bottle import route, request, template, run, debug
from wtforms import Form, SelectField, SubmitField
choices_list = [(u'Jochen',u'Jochen'),(u'G\xfcnter', u'G\xfcnter')]
class MyForm(Form):
name = SelectField(u'Name')
senden = SubmitField(u'Senden')
@route('auswahl')
@route('auswahl', method='POST')
def auswahl():
req = request.forms
form = MyForm(req)
if not req:
return template('auswahl.tpl', form = form, choices_list=choices_list)
else:
return u'Der Name ist {0}'.format(form.name.data)
if __name__ == '__main__':
debug(True)
run(reloader=True)
Code: Alles auswählen
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Bottle WTForms Test</title>
</head>
<body>
<h1>Bottle WTForms Test</h1>
<p>Formular:</p>
<form action="/auswahl" method="post">
%form.name.choices = choices_list
<p>{{!form.name.label}} {{!form.name}}</p>
<p>{{!form.senden}}</p>
</form>
</body>
</html>
Code: Alles auswählen
UnicodeEncodeError('ascii', u'<select id="name" name="name"><option value="Jochen">Jochen</option><option value="G\xfcnter">G\xfcnter</option></select>', 84, 85, 'ordinal not in range(128)')
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 499, in handle
return handler(**args)
File "bottle_wtform_test.py", line 21, in auswahl
req=req,choices_list=choices_list)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1795, in template
return TEMPLATES[tpl].render(**kwargs)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1774, in render
self.execute(stdout, **args)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1762, in execute
eval(self.co, env)
File "/home/jochen/code/test/views/auswahl.tpl", line 13, in <module>
<p>{{!form.name.label}} {{!form.name}}</p>
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1649, in <lambda>
self._str = lambda x: touni(x, enc)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 131, in touni
return x if isinstance(x, unicode) else unicode(str(x), encoding=enc)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 84: ordinal not in range(128)
Code: Alles auswählen
choices_list = [('Jochen','Jochen'),('Günter', 'Günter')]
Code: Alles auswählen
UnicodeDecodeError('ascii', 'G\xc3\xbcnter', 1, 2, 'ordinal not in range(128)')
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 499, in handle
return handler(**args)
File "bottle_wtform_test.py", line 21, in auswahl
req=req,choices_list=choices_list)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1795, in template
return TEMPLATES[tpl].render(**kwargs)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1774, in render
self.execute(stdout, **args)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1762, in execute
eval(self.co, env)
File "/home/jochen/code/test/views/auswahl.tpl", line 13, in <module>
<p>{{!form.name.label}} {{!form.name}}</p>
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 1649, in <lambda>
self._str = lambda x: touni(x, enc)
File "/usr/local/lib/python2.6/dist-packages/bottle-0.8.5-py2.6.egg/bottle.py", line 131, in touni
return x if isinstance(x, unicode) else unicode(str(x), encoding=enc)
File "/usr/local/lib/python2.6/dist-packages/WTForms-0.6.2-py2.6.egg/wtforms/fields.py", line 119, in __str__
return self()
File "/usr/local/lib/python2.6/dist-packages/WTForms-0.6.2-py2.6.egg/wtforms/fields.py", line 135, in __call__
return self.widget(self, **kwargs)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
Gruß, noisefloor