python - flask 处理css文件问题

 mobiledu2502875315 发布于 2022-11-05 09:48

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代码




    Hyun Young | {{ page }}
    


    {% block body %}{% endblock %}    


访问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;
>     }
5 个回答
  • 没猜错的话,你的 CSS 文件里有中文字符,对吧? 把你的 CSS 文件转成 UTF-8 编码就好。

    我没认真看 Traceback ...

    在 from flask 那一行之前加上:

    import sys
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    2022-11-09 16:10 回答
  • 我也遇到了同样的问题,,,上面三个回答也都尝试过了,,还是没解决,,,问下楼主这个是怎么解决的。

    2022-11-09 16:15 回答
  • # -*- coding: utf-8 -*-
    from __future__ import unicode_literals  # 头部加上这句
    from flask import Flask, render_template
    
    2022-11-09 16:17 回答
  • 看起來是常見的編碼問題, unicode跟string之間需要encode或decode的切換, 可以看一下stack trace找一下是哪裡有不是unicode的地方。

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

    2022-11-09 16:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有