简体   繁体   English

如何使用Python Flask重置HTML表单?

[英]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. 我正在构建一个非常简单的Web应用程序(使用Python Flask),该应用程序将向用户显示一些图像并从用户那里得到一些响应。

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 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" . 问题:我想在用户点击“提交”并在列表“ 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Flask-WTForms,如何设置 html 表单部分的样式? - Using Flask-WTForms, how do I style my form section of the html? 如何将数据库 ID 从 flask html 传递到带有 python 的帖子表单中 - How do I pass the database id from flask html into a post form with python Flask WTForms:如何将表单值返回给Python? - Flask WTForms: how do I get a form value back into Python? 如何在Flask(Python)中更新具有选择选项的表单 - How do I update a form having select option in Flask (Python) 如何在我的程序中重置 A 'Score Bank'? Python - How Do I Reset The A 'Score Bank' In My Program? Python 如何在Flask服务器中将python JSON转换为html表? - How do I convert python JSON into a html table in Flask server? Flask - 如何在 Python ZC1C425268E18385D1AB44A 中使用 HTML 按钮的值? - Flask - How do I use the value of an HTML button in a Python function? 如何使用 python/flask 请求 HTML 按钮的名称? - How do I request the name of an HTML button using python/flask? 如何“重置”在使用 Flask 应用程序数据库的 Python 脚本中运行的 db.session? - How do I "reset" the db.session that runs in a Python script that uses a database from a Flask App? 如何在GoDaddy上为我的Python Flask应用配置设置 - How do I configure settings for my Python Flask app on GoDaddy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM