玩蛇网提供最新Python编程技术信息以及Python资源下载!

flask CSS文件'ascii' codec问题

flask 版本使用0.10版本。 文件结构:

/price  
    price.py  
    /templates  
        layout.html  
        index.html  
        rate.html  
    /static  
        style.css  

这是我的python代码:

# -*- coding: utf-8 -*-
from flask import Flask, render_template

app = Flask(__name__) app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
app.config.update(
    # DATABASE = '/flaskr.db',
    DEBUG = True,
    SECRET_KEY = 'freight price',
    USERNAME = 'admin',
    PASSWORD = 'Jake'
    )

@app.route("/")
def index():
    return render_template('index.html', page = "index")

@app.route('/rate')
def rate():
    return render_template('rate.html', page = "rate")

@app.route('/update')
def update():
    return render_template('update.html', page = "update")

if __name__ == '__main__':
    app.run()

layout.html代码

<!doctype html>
<html>
<head>
    <title>Hyun Young | {{ page }}</title>
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
    {% block body %}{% endblock %}    
</body>
</html>

访问CSS的时候出现以下错误: UnicodeDecodeError UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 3: ordinal not in range(128)

Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args)     
File "C:\Python27\lib\site-packages\flask\helpers.py", line 822, in send_static_file cache_timeout=cache_timeout)
File "C:\Python27\lib\site-packages\flask\helpers.py", line 612, in send_from_directory filename = safe_join(directory, filename)
File "C:\Python27\lib\site-packages\flask\helpers.py", line 582, in safe_join return os.path.join(directory, filename)
File "C:\Python27\lib\ntpath.py", line 108, in join path += "\\" + b 

UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in position 3: ordinal not in range(128) The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error. To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.

You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:

dump() shows all variables in the frame dump(obj) dumps all that's known about the object Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.

css代码: 使用UTF-8编码保存

>     *{
>       margin: 0;
>     }
>     p{
>       color: red;
>     }

我也遇到了同样的问题,,,上面三个回答也都尝试过了,,还是没解决,,,问下楼主这个是怎么解决的。

没猜错的话,你的 CSS 文件里有中文字符,对吧? 把你的 CSS 文件转成 UTF-8 编码就好。

我没认真看 Traceback ...

在 from flask 那一行之前加上:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

看起來是常見的編碼問題, unicode跟string之間需要encode或decode的切換, 可以看一下stack trace找一下是哪裡有不是unicode的地方。

刚开始我也遇到了CSS不生效的情况,试着排查了下,发现我把app放在了带有中文字符的文件夹里,移到非中文字符文件夹里就能生效了。

# -*- coding: utf-8 -*-
from __future__ import unicode_literals  # 头部加上这句
from flask import Flask, render_template

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd20115.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月24日 - 17时42分34秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules