Flask Web开发:基于Python的Web应用开发实战(第2版) 问题及解决办法汇总

@[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可以。
IE浏览器Firefox浏览器
在OS X环境下,Safari浏览器也可以显示。

经搜索,解决办法有如下几种:

  1. moment.js 并不在’base.html’中引入,而是在’index.html’中引入:
    VS code1经测试,不好使。

  2. 将scripts块中的代码移到heads块中:
    head经测试不好使。

最终解决办法
原文: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…

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

steventian72

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值