简体   繁体   中英

How do I reset my HTML form with Python Flask?

I'm building a very simple web app (using Python Flask) that will display some images to the user and get some response from the user.

Below is my code (ignoring the other parts of my code not related to this question):

    @app.route('/gallery',methods=['GET','POST'])
   def get_gallery():
        image_names = os.listdir(r'C:\Users\xxxvn\images') 
        b=[str(i)+"|"+str(request.form.get(i)) for i in image_names]
        print (b)            
    return render_template("gallery.html", image_names=image_names)

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
          integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

</head>
<body>


<div class="container">

    <div class="row">

        <div class="col-lg-12">
            <h1 class="page-header">Gallery</h1>
        </div>
        {{image_names}}
        <hr>
        <form method="POST">
        {% for image_name in image_names %}
        <div class="col-lg-3 col-md-4 col-xs-6 thumb">
            <img class="img-responsive" src=" {{url_for('send_image', filename=image_name)}}">
            <input type="radio" name={{image_name}} id="radio1" value="YES" />YES<br>
            <input type="radio" name={{image_name}} id="radio2" value="NO"/>NO<br>          
            <hr>
        </div>
        {% endfor %}
        <input type="submit" name="submit_button" value="SUBMIT"/>
        </form>
    </div>

</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
        integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
        crossorigin="anonymous"></script> 
</body>
</html>

My current app

我当前的应用

Question: I want to reset the form as soon as the user hits submit and capture the output in list "B" . The form should not be resubmitted with old values if the user hits refresh.

Do a request method check and put in your capture login in it.

from flask import request

@app.route('/gallery',methods=['GET','POST'])
def get_gallery():
    image_names = os.listdir(r'C:\Users\xxxvn\images')
    if request.method == "POST":
        b=[str(i)+"|"+str(request.form.get(i)) for i in image_names]
        print (b)          
    return render_template("gallery.html", image_names=image_names)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM