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;
> }
At the beginning, I also encountered the situation where the CSS did not take effect. I tried to troubleshoot and found that I placed the app in a folder with Chinese characters. If I moved it to a folder with non-Chinese characters, it would take effect.
It seems to be a common encoding problem. Encode or decode needs to be switched between unicode and string. You can look at the stack trace to find where there is something that is not unicode.
I also encountered the same problem, and I have tried all three answers above, but still can’t solve it. I would like to ask the author how he solved this problem.
If you guessed it correctly, there are Chinese characters in your CSS file, right? Just convert your CSS files to UTF-8 encoding.I didn’t watch Traceback carefully...
Add before the line from flask: