【问题标题】:FLask : submitting different actions with a dynamically generated formFlask:使用动态生成的表单提交不同的操作
【发布时间】:2021-12-28 20:06:50
【问题描述】:

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

我无法实现的功能是让用户能够接受或拒绝来自其他用户的联系请求(如好友请求)。

在“/contacts”页面上,您可以毫无问题地看到您从其他用户收到的所有联系请求,但我想添加接受或拒绝它们的选项,这就是我卡住的地方。

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

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

我想过做相反的事情并将标签的name设置为请求的ID,并将value设置为“删除”或“接受”,但是在服务器端我不知道是什么使用 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")

【问题讨论】:

    标签: python flask


    【解决方案1】:

    这就是我的做法:

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

    @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>
    

    注意我如何在提交按钮周围使用&lt;a&gt;&lt;/a&gt; 标记,将Jinja2 url_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")
    

    【讨论】:

    • 唉,它不适用于我的 /manage_requests 路由的 POST 方法,我尝试在 ```
      ``` 中使用您的代码,而不是像这样使用 &lt;a&gt;:@ 987654332@request.form.get``` 在 manage_requests 路由中,但该路由没有接收到传递给 url_for 的额外数据(“action”和“request_id”)。有什么想法吗?
    • 好的我已经解决了我的问题,非常感谢你的帮助,按照你的建议,我已经没有太多事情要做了,所以我把&lt;a&gt;标签换成了上面提到的&lt;form&gt; ,但是为了在服务器端接收额外的参数 action 和 request_id,它们必须像这样包含在路由中:@app.route("/manage_requests/&lt;request_id&gt;/&lt;action&gt;", methods = ["POST"]) @login_required def manage_requests(request_id, action): 而不是使用request.form.get() 现在可以正常工作了!非常感谢您的帮助:D
    猜你喜欢
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    相关资源
    最近更新 更多