FLask:使用动态生成的表单提交不同的操作



我正在用sqlite数据库构建一个基本的Flask应用程序。

我在实现时遇到的问题是,用户可以接受或拒绝另一个用户的联系请求(比如朋友请求(。

在"/联系人";页面上,你可以毫无问题地看到你从其他用户那里收到的所有联系请求,但我想添加接受或拒绝他们的选项,这就是我的困境。

通过从数据库中获取当前用户收到的所有联系人请求并将其显示在页面上,可以动态生成表单。

我尝试为每个请求使用两个<input type="submit" name="accept/delete" value="id_of_the_request">标签,一个带有accept选项,另一个带有delete选项,这两个标签都指向相同的路由,但与其他一些输入类型不同;值";属性控制按钮上显示的文本,所以我不能将其设置为,例如,联系人请求的id(我在下面的代码中做了此操作(,因为这样我会在页面上看到两个带数字的按钮。

我想做相反的事情,改为将标记的name设置为请求的id,并将value设置为"0";删除";或";接受";,但在服务器端,我不知道request.form.get()的名称,因为请求的id是根据数据库中的内容动态生成的。

我觉得我错过了一些基本知识,但这应该不会太难。

这是我的html代码(模板从数据库中传递了一个字典(请求(列表,对应于当前用户收到的联系人请求列表。每个请求由3列组成:request_id、user_email、contact_email。request_id是主键,user_email是发送请求的人的电子邮件,而contact_email是接收请求的人。(:

<form action="/manage_requests" method="post">
<ul>
{% for request in requests %}
<li>{{request.user_email}} sent you a contact request.</li>
<input type="submit" name="accept" value="{{request.r_id}}">
<input type="submit" name="refuse" value="{{request.r_id}}">
{% endfor %}
</ul>
</form>

以下是我处理接受或拒绝请求的python代码:

@app.route("/manage_requests", methods = ["POST"])
@login_required
def manage_requests():
acceptedID = int(request.form.get("accept"))
refusedID = int(request.form.get("refuse"))

## Add the user who sent the request as a contact for both them and us, then delete the request.
if acceptedID :
# fetch the info of the request corresponding id from the database requests table 
# get the sender's user_email
# insert the data into the database contacts table for both the sender and the receiver (current user)
# delete the request from the requests table in the database
return redirect("/contacts")

## Delete the request
elif refusedID :
# delete the request from the database requests table
return redirect("/contacts")

我会这样做:

首先是一条像你一样返回所有联系请求的路线:

@app.route("/contacts", methods = ["POST"])
@login_required
def contacts():
# requests_list = Find all the requests for a user 
return render_template('contacts.html', requests_list=requests_list)

然后我在模板中返回所有找到的请求:

<ul>
{% for request in requests %}
<li>{{request.user_email}} sent you a contact request.</li>
<a href="{{ url_for('manage_requests', request_id=request.r_id, action='accept' )}}">
<input type="submit" name="accept">
</a>
<a href="{{ url_for('manage_requests', request_id=request.r_id, action='refuse' )}}">
<input type="submit" name="refuse">
</a>
{% endfor %}
</ul>

请注意,我是如何在提交按钮周围使用<a></a>标记的,将Jinja2url_for函数传递给href属性,将request_id作为参数,并将变量action作为值acceptrefuse:

最后:

@app.route("/manage_requests", methods = ["POST"])
@login_required
def manage_requests():
action = request.args.get('action')
request_id = int(request.args.get('request_id'))

## Add the user who sent the request as a contact for both them and us, then delete the request.
if action == "accept" :
# fetch the info of the request corresponding id from the database requests table 
# get the sender's user_email
# insert the data into the database contacts table for both the sender and the receiver (current user)
# delete the request from the requests table in the database
return redirect("/contacts")

## Delete the request
else :
# delete the request from the database requests table
return redirect("/contacts")

最新更新