Python 从jinja变量动态传递变量
我试图在ajax调用中传递一个jinja变量。我正在调用gerSlots路由。我希望URL发布到/getAppoints/,但它正在打印{{userName}Python 从jinja变量动态传递变量,python,ajax,flask,jinja2,Python,Ajax,Flask,Jinja2,我试图在ajax调用中传递一个jinja变量。我正在调用gerSlots路由。我希望URL发布到/getAppoints/,但它正在打印{{userName} @app.route('/getAppointments/<userName>', methods=['POST']) def getSlots(userName): requestedDate=request.get_json() print(userName) print(requestedDat
@app.route('/getAppointments/<userName>', methods=['POST'])
def getSlots(userName):
requestedDate=request.get_json()
print(userName)
print(requestedDate)
return redirect(url_for('appointments',rDate=requestedDate,userName=userName))
$.ajax({
type: "POST",
url: "{{ url_for('getSlots',userName= '{{ userName }}')}}",
data: JSON.stringify(date.format()) ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
}
@app.route('/getAppointments/',methods=['POST'])
def getSlots(用户名):
requestedDate=request.get_json()
打印(用户名)
打印(请求日期)
返回重定向(url_for('appointment',rDate=requestedDate,userName=userName))
$.ajax({
类型:“POST”,
url:“{url_for('getSlots',userName='{{userName}')}”,
数据:JSON.stringify(date.format()),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
警报(数据);
}
});
}
这是一个简单的解决方案
删除{}
表单用户名。
您正在为方法使用jinjaurl\u中的username
jinja变量。这意味着您可以使用用户名
,而不使用{{}