How I can get many html forms from a single page, in one flask function

前端 未结 1 1031
生来不讨喜
生来不讨喜 2021-01-23 07:58
@app.route(\'/profile\',methods=[\'POST\',\'GET\'])
def profile(id):
      id13=session[\'id\']
      id_profile=id
      search=None
      row=None
               


        
相关标签:
1条回答
  • 2021-01-23 08:29

    Only one form can be submitted by the browser. You're trying to process the data from both, but the non-submitted data won't exist in request.form and will raise a 400 error.

    You need to be able to distinguish which form was submitted. Add a name and value to the submit button and check which value was returned to know what processing to do. You were on the right track by adding buttons with names, but you weren't consistent about them and weren't checking their value in Flask.

    <!-- in the search form -->
    <button type=submit name=action value=search>
    
    <!-- in the second form -->
    <button type=submit name=action value=comment>
    
    if request.method == 'POST':
        if request.form['action'] == 'search':
            # do search action
        elif request.form['action'] == 'comment':
            # do comment action
    

    In this case, it makes more sense to have different views handling searching and commenting. Create two separate views, and point the forms at the correct URLs.

    <form method=post action="{{ url_for('search') }}">
    
    <form method=post action="{{ url_for('comment') }}">
    
    0 讨论(0)
提交回复
热议问题