Python烧瓶:单击通过单击另一个按钮渲染的按钮

Python烧瓶:单击通过单击另一个按钮渲染的按钮,python,flask,Python,Flask,我是Flask的新手,我正在尝试构建一个简单的web应用程序。基本上,我在主页上有一个文本输入框和一个提交按钮。单击“提交”后,它会根据输入的文本显示一些结果(目前它在下面的代码中硬编码) 我想要的是,当你点击提交时,它不仅应该显示结果,还应该显示一个按钮,将输入的文本添加到特定的文件中。然而,我正在努力让它工作(“添加到数据集”按钮没有任何作用) 以下是我目前的情况: app = Flask(__name__) @app.route('/', methods=['GET', 'POST'])

我是Flask的新手,我正在尝试构建一个简单的web应用程序。基本上,我在主页上有一个文本输入框和一个提交按钮。单击“提交”后,它会根据输入的文本显示一些结果(目前它在下面的代码中硬编码)

我想要的是,当你点击提交时,它不仅应该显示结果,还应该显示一个按钮,将输入的文本添加到特定的文件中。然而,我正在努力让它工作(“添加到数据集”按钮没有任何作用)

以下是我目前的情况:

app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        return render_template('index.html')

    # if submit button is clicked
    if request.form['submit'] == 'Submit':
        # get the text from the form that was filled in
        input_text = request.form['text']

        final_result = 'stackoverflow rocks'

        return render_template('index.html', result=final_result, text=input_text)

    if request.form['add-dataset'] == 'Add to dataset':
        f = open('dataset/dataset.tsv', 'a')
        f.write(input_text)
index.html:

<!doctype html>
<head>
    <link rel="stylesheet" media="screen" href ="static/bootstrap.min.css">
    <link rel="stylesheet" href="static/bootstrap-theme.min.css">
    <meta name="viewport" content = "width=device-width, initial-scale=1.0">
</head>
<br/>
<div class="container">
    <form action="/" method="post" role="form">
        <div class="form-group">
            <label for="text">Text:</label>
            <input type="text" class="form-control" name="text" placeholder="Input sentence here">
            <br />
        </div>
        <input type="submit" name="submit" value="Submit" class="btn btn-success">
    </form>
    <br/>

    {% if result is not none %}
        <div class="alert alert-success">
            {{ result }}
        </div>
        <h2>Add to dataset</h2>
        <br/><input type="submit" name="add-dataset" value="Add to dataset" class="btn btn-success">
    {% endif %} 
</div>
</html>


正文:

{%如果结果不是none%} {{result}} 添加到数据集
{%endif%}
只是想知道。。。为什么不能将onClick属性添加到按钮中,以便在单击按钮时可以处理以下操作

HTML:

第二

 <input>
html:


正文:


{%如果结果不是none%} {{result}} 添加到数据集
正文:
{%endif%}
然而,我正在努力让它正常工作。这到底是什么意思?更具体地解决您的实际问题。我上面使用的代码不起作用,它出于某种原因抱怨缩进。我知道这样不正确,因为它实际上是另一个表单中的一个表单(在本例中只是一个按钮),但我不知道如何做。。。正如我所说,我对Flask完全是新手。我理解,但如果我们不知道您的问题是什么,我们将很难帮助您。对不起,缩进不是问题所在。问题在于,单击新按钮“添加到数据集”没有任何作用。我编辑了我的问题:)谢谢你的回答。在这种情况下,是否也可以在屏幕上显示消息?对不起,我根本不习惯web开发,而且我对Flask还不熟悉。你能告诉我如何做JS部分吗?是的,谷歌关于flask flash,flash中有一个方法叫做get_flashed_messages()。用法很简单。。按照此链接显示信息Ok谢谢。你能告诉我怎么做JS部分吗?我对JS一点也不熟悉对不起,这是什么意思?很抱歉,我不是web开发人员。好的,谢谢你,但是我如何将Python代码添加到JS代码中?因此,在Python中打开一个文件并将输入的文本附加到其中。当我将
input\u text=request.form['text']
放在
if request.form['submit']=='submit':
之前时,我得到了错误
werkzeug.exceptions.HTTPException.wrap..newcls:400错误请求:KeyError:'text'
,我知道原因。首先在文本框中输入文本,然后单击“提交”,但单击“提交”时,文本框中的文本将消失。因此,当您尝试单击正/负按钮时,它会尝试从文本框中获取文本,但没有。什么正/负按钮?为什么在“if request”之前有它对不起,我是说“add to dataset”按钮你想用1个输入表单和2个按钮吗?如果是这样,那么使用我的python,但是删除整个第二个表单,只需将输入按钮从第二个添加到第一个
@app.route("/somepath",methods=["GET","POST"])
def handler():
    data =  request.get_json()
    //process the data here
 <input>
 <form>
if request.method=='POST':
    input_text=request.form['text']
    if request.form['submit']=='Submit':
  final_result = 'stackoverflow rocks'
  return render_template('index.html', result=final_result, text=input_text)
    elif request.form['action']=='addToDataset':
  f = open('dataset/dataset.tsv', 'a')
  f.write(input_text)
        return redirect(url_for('index'))
    else:
        render_template('index.html')
else:
    return render_template('index.html')
  <div class="container">
        <form method="post" role="form">
            <div class="form-group">
                <label for="text">Text:</label>
                <input type="text" class="form-control" name="text" placeholder="Input sentence here">
                <br />
            </div>
            <input type="submit" name="submit" value="Submit" class="btn btn-success">
        </form>
        <br/>

        {% if result is not none %}
            <div class="alert alert-success">
                {{ result }}
            </div>
            <h2>Add to dataset</h2>
            <br/>
            <form method="post" role="form">
                <div class="form-group">
                    <label for="text">Text:</label>
                    <input type="text" class="form-control" name="text" placeholder="Input sentence here">
                    <br />
                </div>
                <input type="submit" name="submit" value="addToDataset" class="btn btn-success">
            </form>
        {% endif %} 
    </div>