Heim > Fragen und Antworten > Hauptteil
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;
> }
阿神2017-04-17 11:39:54
看起來是常見的編碼問題, unicode跟string之間需要encode或decode的切換, 可以看一下stack trace找一下是哪裡有不是unicode的地方。
阿神2017-04-17 11:39:54
# -*- coding: utf-8 -*-
from __future__ import unicode_literals # 头部加上这句
from flask import Flask, render_template
阿神2017-04-17 11:39:54
没猜错的话,你的 CSS 文件里有中文字符,对吧?
把你的 CSS 文件转成 UTF-8 编码就好。
我没认真看 Traceback ...
在 from flask 那一行之前加上:
import sys
reload(sys)
sys.setdefaultencoding("utf-8")