我正在用燒瓶寫一個小網頁,我偶然發現了奇怪的錯誤。燒瓶形式數據類型錯誤(unicode數據不是unicode)
以簡單的形式,我想傳遞3個字符串值。當我試圖通過任何特殊字符(如「L」,「A」,等等),瓶給我的回溯:
127.0.0.1 - - [18/Aug/2015 16:16:32] "POST /add_tutorial HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/werkzeug/contrib/fixers.py", line 148, in __call__
return self.app(environ, start_response)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pawel/repos/tiktalik-www-panel/panel.py", line 123, in add_tutorial
author=data['author']
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/writing.py", line 784, in add_document
perdocwriter.add_column_value(fieldname, column, cv)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/codec/base.py", line 821, in add_column_value
self._get_column(fieldname).add(self._docnum, value)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/columns.py", line 251, in add
self._dbfile.write(v)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/filedb/structfile.py", line 99, in write
return self.file.write(*args, **kwargs)
File "/home/pawel/PANEL/flask/lib/python2.7/site-packages/whoosh/filedb/compound.py", line 325, in write
bio.write(inbytes)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in position 10: ordinal not in range(128)
其崩潰的代碼:
def add_tutorial():
if request.method == 'POST':
ix = indexing(INDEXDIR,BaseSchema,'tutorials')
with ix.writer(optimize = True) as writer:
data = {}
data['title'] = request.form['title'] #here it crashes
app.logger.debug(type(request.form['title']))
data['author'] = request.form['author']
data['difficulty'] = int(request.form['difficulty'])
data['fileOfArt'] = request.files['file']
data['pictures'] = request.files.getlist("pictures")
data['lang'] = request.form['lang']
data['tags'] = parse_tags(request.form['tags'])
if data['title'] and data['fileOfArt']:
subdir = plToAng(data['title'])
subdir = subdir.replace(" ","_")
content = data['fileOfArt'].read()
for picture in data['pictures']:
handle_file(picture,subdir)
content = repl_pictures(content,data['pictures'],subdir)
writer.add_document(
title=data["title"],
sort_title=data["title"].lower(),
content=content,
date=datetime.now(),
url=subdir,
difficulty=data['difficulty'],
lang=data['lang'],
tags=data['tags'],
author=data['author']
)
ix.close()
return redirect(url_for('add_tutorial', title=data['title'],\
difficulty=data['difficulty']))
當我嘗試強制request.form ['標題']編碼爲「UTF-8」我得到錯誤,「TypeError:解碼Unicode不支持」(邏輯,因爲數據傳遞是Unicode)。
有沒有解決此問題的方法? (我不能放棄特殊字符)
這似乎並非是一個ISSU e與Flask,但與飛快。請發佈*完整回溯*。 –
我已經編輯完全回溯而不是錯誤的帖子 –
所有'data'成員的類型是什麼? –