2013-08-20 41 views
0

我得到UnicodeDecodeError误差FileAdmin模块(包含在Flask-Admin库瓶):Unicode的错误,而在烧瓶管理员创建目录

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 5: ordinal not in range(128) 

每当我创造出具有具有非ASCII字符的目录它的名字(如très),我得到那个错误。

我认为这一点是添加UTF-8编码:

# -*- coding: utf-8 -*- 

但其中(哪些文件?),以及如何内FileAdmin module处理呢?这个我不清楚。

编辑以下@PaoloCasciello要求,找出错误回溯下面

Traceback (most recent call last) 

File "C:\Python27\lib\site-packages\flask\app.py", line 1701, in __call__ 
return self.wsgi_app(environ, start_response) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1689, in wsgi_app 
response = self.make_response(self.handle_exception(e)) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1687, in wsgi_app 
response = self.full_dispatch_request() 
File "C:\Python27\lib\site-packages\flask\app.py", line 1360, in full_dispatch_request 
rv = self.handle_user_exception(e) 
File "C:\Python27\lib\site-packages\flask\app.py", line 1358, in full_dispatch_request 
rv = self.dispatch_request() 
File "C:\Python27\lib\site-packages\flask\app.py", line 1344, in dispatch_request 
return self.view_functions[rule.endpoint](**req.view_args) 
File "C:\Python27\lib\site-packages\flask_admin\base.py", line 59, in inner 
return f(self, **kwargs) 
File "C:\Python27\lib\site-packages\flask_admin\base.py", line 59, in inner 
return f(self, **kwargs) 
File "C:\Python27\lib\site-packages\flask_admin\contrib\fileadmin.py", line 460, in index 
actions_confirmation=actions_confirmation) 
File "C:\Python27\lib\site-packages\flask_admin\base.py", line 247, in render 
return render_template(template, **kwargs) 
File "C:\Python27\lib\site-packages\flask\templating.py", line 125, in render_template 
context, ctx.app) 
File "C:\Python27\lib\site-packages\flask\templating.py", line 107, in _render 
rv = template.render(context) 
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 969, in render 
return self.environment.handle_exception(exc_info, True) 
File "C:\Python27\lib\site-packages\jinja2\environment.py", line 742, in handle_exception 
reraise(exc_type, exc_value, tb) 
File "C:\Flask\test\templates\admin\file\list.html", line 3, in top-level template code  
{% import 'admin/actions.html' as actionslib with context %} 
File "C:\Flask\test\templates\admin\master.html", line 1, in top-level template code 
{% extends admin_base_template %} 
File "C:\Flask\test\templates\admin\base.html", line 22, in top-level template code 
{% block page_body %} 
File "C:\Flask\test\templates\admin\base.html", line 40, in block "page_body" 
{% block body %}{% endblock %} 
File "C:\Flask\test\templates\admin\file\list.html", line 24, in block "body" 
{% block file_list_table %} 
File "C:\Flask\test\templates\admin\file\list.html", line 42, in block "file_list_table" 
{% block list_row scoped %} 
File "C:\Flask\test\templates\admin\file\list.html", line 51, in block "list_row" 
{% block list_row_actions scoped %} 
File "C:\Flask\test\templates\admin\file\list.html", line 61, in block "list_row_actions" 
<input type="hidden" name="path" value="{{ path }}"></input> 
File "C:\Python27\lib\site-packages\markupsafe\_native.py", line 22, in escape 
return Markup(text_type(s) 
+0

如果您没有提供您的代码,那么我们可以帮到您。 – Shervin

+0

@Shervin:这是'FileAdmin模块'[代码](http://flask-admin.readthedocs.org/en/latest/_modules/flask/ext/admin/contrib/fileadmin/)....当然这不是我的代码! – mannaia

+0

请提供完整的追溯。 –

回答

1

这是发生因为Python os.walkdir返回文件名以ASCII时使用ASCII路径调用。

所以,当你初始化FileAdmin,请确保您传递的基本路径为unicode字符串::

admin.add_view(unicode(path), '/files/', name='Files') 

最新瓶,管理员有这个固定 - FileAdmin将迫使Unicode进行内部的路径。

+0

好的,很有用,非常感谢! – mannaia

+0

顺便说一句,当上传名称包含非ascii字符的文件时,我遇到了同样的问题。我应该怎么做呢? – mannaia

+0

我在做一个愚蠢的错误......现在我意识到你的答案也适用于上传文件。谢谢 ! – mannaia