Skip to main content
 首页 » 编程设计

python之Flask 表单数据类型错误(unicode 数据不是 unicode)

2025年05月04日9fff_TT

我正在使用 Flask 编写一个小网页,我偶然发现了一个奇怪的错误。

我想以简单的形式传递 3 个字符串值。当我尝试传递任何特殊字符(如“ł”、“±”等)时,flask 给我回溯:

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['title'] 编码为“UTF-8”时,我收到错误消息“TypeError:不支持解码 Unicode”(合乎逻辑,因为传递的数据是 Unicode)。

有什么办法可以解决这个问题吗? (我不能放弃特殊字符)

请您参考如下方法:

您应该将具有特殊字符的字符串编码为 utf-8 字节字符串:

mynewstring = mystring.encode('utf-8') 

结果是这样的:

>>> a = u"عاصم" 
>>> a.encode('utf-8') 
'\xd8\xb9\xd8\xa7\xd8\xb5\xd9\x85'