简体   繁体   English

如何在不使用 forms 的情况下将客户端参数传递给服务器端路由?

[英]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.

相关问题 服务器端(python)和客户端(javascript)设计与交互 - Server-side(python) and client-side(javascript) design and interaction Firebase(客户端与服务器端) - Firebase (client-side vs server-side) 将服务器端计算与客户端可视化联系起来 - connect server-side computing with client-side visualization 如何在客户端和服务器端使用验证规则? - How to use the validation rules on both client-side and server-side? 使用JavaScript和Google App Engine中的服务器端Python代码动态生成客户端HTML表单控件 - Dynamically generate client-side HTML form control using JavaScript and server-side Python code in Google App Engine 将文本从客户端发送到服务器端程序进行处理 - Sending text from client-side to a server-side program for processing 获取数据并将其发布到外部Web API(服务器端还是客户端)? - Getting and posting data to external web API, server-side or client-side? Django:避免服务器端和客户端验证代码重复 - Django: Avoiding Server-Side and Client-Side Validation Code Duplication 在Django中,我如何执行服务器端功能并在客户端上监视进度 - In Django, how do I perform server-side functions and monitor progress on client side 如何使用服务器端脚本生成网页的屏幕截图? - How can I generate a screenshot of a webpage using a server-side script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM