python - 为什么 render_template 没有被表达?
问题描述
我有一个使用 HTML 页面设置的工作烧瓶应用程序。问题是,在我拥有的 HTML 中{{ Appointments }}
,它总是显示render_template
空列表的 second 的值。
@app.route("/PatientDashboard.html", methods=["GET", "POST"])
def PatientDashboard():
if request.method == "POST":
Date = request.form.get("Date")
print(Date)
return render_template("PatientDashboard.html", Appointments=["This should show up."])
else:
return render_template("PatientDashboard.html", Appointments=[])
问题是第一个render_template
从未表达过。为什么会这样,我将如何解决它?
非常感谢你。
编辑1:
相关的 HTML 如下。
<script>
var jsDate = $('#calendar').datepicker({
inline: true,
firstDay: 1,
showOtherMonths: false,
dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
onSelect: function(dateText, inst) {
document.getElementById("Date").innerHTML = dateText;
$.ajax({
type: 'POST',
url: "{{ url_for('PatientDashboard') }}",
data: {Date: dateText},
dataType: "text",
});
}
});
</script>
此外,我有{{ Appointments }}
一个分隔线。
解决方案
您正在将渲染模板的内容与Appointments
您的 POST 请求的响应一起获取。如果您想Appointments
在页面上使用数据,您需要使用回调扩展您的 POST 请求,该回调将使用该数据来满足您的任何需求。
所以基本上会发生什么:
- 页面加载(GET 请求),模板以空
Appointments
列表呈现 - 页面触发 POST ajax 请求,该请求返回带有
Appointments
集合的渲染模板 - 您没有处理 POST 响应,因此这些数据只是被丢弃。
典型的方法是从 POST 响应中仅获取相关数据(例如 JSON 格式),而不是整个页面:
from flask import Response
if request.method == "POST":
Date = request.form.get("Date")
print(Date)
Appointments=["This should show up."]
return Response(json.dumps(Appointments), mimetype='application/json')
else:
return render_template("PatientDashboard.html", Appointments=[])
推荐阅读
- javascript - 用下拉值替换函数变量(lodash)
- flutter - RenderIndexedSemantics 对象在布局期间被赋予无限大小
- laravel - Laravel Lighthouse:CanDirective:如何定义自定义错误消息?
- windows-terminal - WT 与配置文件最大化
- css - 带类的 Css ID - 父选择器
- asp.net - 在 asp.net 核心中连接到 neo4j
- python - 为图像对比度度量实现直方图扩展
- python-3.x - 在 odoo 中确认订单后发送电子邮件
- c++ - 无法构建 qtdeclarative(为 Raspberry Pi 零 W 交叉编译 Qt5):错误“PATH_MAX”未在此范围内声明
- firebird - 通过 isql 实用程序使用十六进制二进制字符串更新 Firebird blob