@[TOC](Flask Web开发:基于Python的Web应用开发实战(第2版)问题及解决办法汇总
问题一:2.4节,Web开发服务器, set FLASK_APP=hello.py 遇到的问题及解决办法
按书上写的,在windows环境下运行如下指令,即可启动开发版的web服务器:
(venv) $ set FLASK_APP=hello.py
我的环境时Windows 10 的Powershell,提示错误如下:
(venv) PS D:\flasky> set FLASK_APP = "hello.py"
Set-Variable : 找不到接受实际参数“hello.py”的位置形式参数。
所在位置 行:1 字符: 1
+ set FLASK_APP = "hello.py"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-Variable],ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetVariableCommand
根据stackoverflow的指导,改为:
(venv) PS D:\flasky> $env:FLASK_APP = "hello.py"
即可。
另外,对第二章第四节的标题,提出翻译上的异议,应该改为“开发版web服务器”,因为在Flask的web服务器运行时,明确的说明了:
WARNING: This is a development server. Do not use it in a production deployment.
这就明确的说明了,Flask自带的web服务器只用于开发目的,而不适合于商业发布。所以2.4节的标题不应该时开发web服务器,而是开发版web服务器。
问题二:3.6节,The local date and time is {{ moment(current_time).format(‘LLL’) 无法显示时间
书上的原始代码:
示例 3-12 templates/base.html:引入 Moment.js 库
{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}
示例 3-13 hello.py:添加一个 datetime 变量
@app.route('/')
def index():
return render_template('index.html', current_time=datetime.utcnow())
代码 3-14 templates/index.html:使用 Flask-Moment 渲染时间戳
<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
在Windows环境下,IE浏览器无法显示系统当前时间。Firefox可以。
在OS X环境下,Safari浏览器也可以显示。
经搜索,解决办法有如下几种:
-
moment.js 并不在’base.html’中引入,而是在’index.html’中引入:
经测试,不好使。
-
将scripts块中的代码移到heads块中:
经测试不好使。
最终解决办法
原文:python3.5使用flask-moment无法显示时间戳的问题
下载moment.min.js 或者 moment.js 或者 oment-with-locales.min.js,并放在static/js目录。
下载地址:moment.js
将原书代码:
{{moment.include_moment()}}
改为:
{{moment.include_moment(local_js=url_for('static',filename='js/moment-with-locales.min.js')) }}
完美解决问题。
坑爹的IE…