[英]How can I pass a client-side parameter to a server-side route without using forms?
I have a simple Flask
web app.我有一个简单的
Flask
web 应用程序。 My index template has various ways of interacting with clients using javascript and HTML
.我的索引模板有多种使用 javascript 和
HTML
与客户端交互的方式。 I am also have a form that, upon submission, routes to another flask process and uses the request.form
command to retrieve user-submitted data.我还有一个表单,在提交后,它会路由到另一个 flask 进程,并使用
request.form
命令检索用户提交的数据。
However, I want to do something a little different.但是,我想做一些不同的事情。 I would like to initiate a
Flask
redirection upon javascript
event but include a parameter, and not use form .我想在
javascript
事件时启动Flask
重定向,但包含一个参数,而不是使用form 。
For example, my index.html
file would display something like this after template rendering:例如,我的
index.html
文件在模板渲染后会显示如下内容:
function startRedirect(parameter) {
window.location.pathname = '/myRedirect';
}
<input type="checkbox" id="sample" name="sample" onChange="startRedirect(parameter);">
And part of my Flask script would have:我的 Flask 脚本的一部分将具有:
@app.route('/myRedirect')
def myRedirectFunction():
# do something with the parameter here
return render_template('index.html')
I realize this can be done with using a form , but I am interested in accomplishing this task without having a form .我意识到这可以通过使用表单来完成,但我有兴趣在没有表单的情况下完成这项任务。 I was thinking about somehow using
request.args
, but don't quite understand what to do.我正在考虑以某种方式使用
request.args
,但不太明白该怎么做。
You can use a dynamic route to capture a simple input and pass it to the route's function.您可以使用动态路由来捕获简单的输入并将其传递给路由的 function。
app.route('/myRedirect/<param>')
def myRedirectFunction(param='hello world'):
return render_template('index.html', param=param)
Using this route as a redirect, you can pass a single param
(or multiple if you serialize them) that you can use to do something .使用此路由作为重定向,您可以传递一个
param
(或多个,如果您将它们序列化),您可以使用它来做某事。 From there, you can either display or you can redirect again to a common endpoint so the user does not see the param in the url.从那里,您可以显示或再次重定向到公共端点,这样用户就不会看到 url 中的参数。
There's no need for a form or an explicit redirect
, just attach a route and some parameter to the dynamic route.不需要表单或显式
redirect
,只需将路由和一些参数附加到动态路由即可。
Let's say you have a model to list the departments in your company:假设您有一个 model 来列出您公司的部门:
class Departments(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
Now you have a department selection page:现在你有一个部门选择页面:
@app.route('/departments_home', methods=['GET'])
def departments_home():
departments = Departments.query.all()
return render_template('departments_home.html',
departments=departments)
On the frontend you might have a variety of selections, each giving a link to the same route but with a different department_id:在前端,您可能有多种选择,每种选择都提供指向同一路线但具有不同部门 ID 的链接:
{% for department in departments %}
<a href="{{ url_for('load_department', department_id=department.id) }}" class="button">Click to go to {{ department.name }}</a>
{% endfor %}
Now you just need another route to handle this, taking the variable department_id that was passed in the GET request:现在您只需要另一条路线来处理这个问题,获取在 GET 请求中传递的变量 department_id:
@app.route('/load_department/<department_id>', methods=['GET'])
def load_department(department_id):
department = Departments.query.get(int(department_id))
department_data = # do stuff here with the specific department
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.