【问题标题】:Python Flask: click button that rendered by clicking another buttonPython Flask:单击通过单击另一个按钮呈现的按钮
【发布时间】:2018-07-23 12:31:14
【问题描述】:

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

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

这是我现在所拥有的:

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>

【问题讨论】:

  • 但是,我很难让它发挥作用。 这到底是什么意思?更具体地解决您的实际问题。
  • 我上面使用的代码不起作用,它出于某种原因抱怨缩进。我知道这样是不正确的,因为它实际上是另一个表单中的一个表单(在这种情况下只是一个按钮),但我不知道该怎么做......正如我所说,我对 Flask 完全陌生。
  • 我明白,但如果我们实际上不知道您的问题是什么,我们将很难为您提供帮助。
  • 抱歉,缩进不是问题。问题只是单击新按钮“添加到数据集”没有任何作用。我编辑了我的问题:)

标签: python flask


【解决方案1】:

只是想知道......为什么你不能将 onClick 属性添加到按钮,所以当你点击它时,你会处理类似的事情

HTML:

<input type="submit" onclick="(function(){
                               alert('Hey i am calling');
                               })();" />

使用 fetch API 代替 alert 将数据发送到服务器或做任何你想做的事情

API

@app.route("/somepath",methods=["GET","POST"])
def handler():
    data =  request.get_json()
    //process the data here

【讨论】:

  • 感谢您的回复。在这种情况下是否也可以在屏幕上显示消息?抱歉,我根本不习惯 Web 开发,而且我是 Flask 的新手。你能告诉我JS部分怎么做吗?
  • 是的,google一下flask flash,flash中有一个方法叫做get_flashed_messages()。用法很简单..点击这个链接显示消息flask.pocoo.org/docs/1.0/patterns/flashing
  • 好的,谢谢。你能告诉我如何做JS部分吗?我对JS一点也不熟悉抱歉。
  • 这是什么意思?抱歉,我不是网络开发人员。
  • 好的,谢谢,但是如何将 Python 代码添加到该 JS 代码中?所以要在 Python 中打开一个文件并将输入的文本附加到它。
【解决方案2】:

第二个

 <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')

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>

【讨论】:

  • 当我将input_text=request.form['text'] 放在if request.form['submit']=='Submit': 之前时,我得到了错误werkzeug.exceptions.HTTPException.wrap.&lt;locals&gt;.newcls: 400 Bad Request: KeyError: 'text',我知道为什么。您首先在文本框中输入文本,然后单击提交,但是当您单击提交时,文本框中的文本消失了。因此,当您尝试单击正/负按钮时,它会尝试从文本框中获取文本,但没有。
  • 什么正面/负面按钮?以及为什么在'if request'之前有它
  • 对不起,我的意思是“添加到数据集”按钮
  • 您要使用 1 个输入表单和 2 个按钮吗?如果是这样,那么使用我的python,但删除整个第二个表单,只需将输入按钮从第二个添加到第一个
猜你喜欢
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多