【问题标题】:Python Flask: clicking button doesn't do anythingPython Flask:单击按钮不执行任何操作
【发布时间】:2018-07-23 14:23:05
【问题描述】:

我是 Flask 的新手,我正在尝试构建一个简单的网络应用程序。基本上我在主页上有一个文本输入框和一个提交按钮。

点击提交后,它会根据输入的文本显示一些结果(现在它已在下面的代码中硬编码)以及 2 个按钮(正/负)将输入的文本添加到特定文件(带有“正面”或“负面”标签)。

但是,我面临的问题是这 2 个按钮:单击时它们什么都不做。

这是我现在拥有的:

Python Flask 应用程序

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

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

    if request.method == 'POST':
        # get the text from the form that was filled in
        input_text = request.form['text']

        # if submit button is clicked
        if request.form['submit'] == 'Submit':
            final_result = 'stackoverflow is the best'

        if request.form['submit'] == 'Positive':
            f = open('dataset/dataset.tsv', 'a')
            f.write(input_text + '\t' + 'positive')

        # if negative button is clicked  
        if request.form['submit'] == 'Negative':
            f = open('dataset/dataset.tsv', 'a')
            f.write(input_text + '\t' + 'negative')

        # show the result on the page   
        return render_template('index.html', result=final_result, text=input_text)

index.html 文件

<!doctype html>
<form action="/process-data" method="post" role="form">
    <label for="text">Text:</label>
    <input type="text" name="text" placeholder="Input sentence here">
    <input type="submit" name="submit" value="Submit">
</form>

{% if result is not none %}
    {{ result }}
    <h2>Add to dataset</h2>
    <form action="/process-data" method="post" role="form">
        <label for="add-dataset">This sentence was:</label>
        <input type="submit" name="submit" value="Positive">
        <input type="submit" name="submit" value="Negative">
    </form>
{% endif %} 
</html>

【问题讨论】:

    标签: python flask


    【解决方案1】:

    创建您的/ 路由并让它简单地返回索引模板,而不是这样:

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

    然后将另一条路由分配给另一个将进行处理的函数。我不会称它为 index,可能是类似 process-data 之类的东西,像这样:

    @app.route('/process-data', methods=['GET', 'POST'])
    def process_data():
        # put the body of your function here
        # ...
        # ...
        # ...
        return render_template('index.html', result=final_result, text=input_text)
    

    最后,您只需要相应地更新您的表单操作:

    <form action="/process-data" method="post" role="form">
    

    【讨论】:

    • 我尝试了您的解决方案,但现在它显示“分配前引用了局部变量 'input_text'”。如何将变量从一个表单(即带有文本框和提交按钮的第一个表单)传递到第二个表单(用于将 input_text 写入带有标签的文件的正/负按钮)?
    • 我需要查看您的 process_data 函数的主体才能知道您做错了什么。
    • 我用新代码编辑了我的初始帖子。请检查一下好吗?
    【解决方案2】:

    检查您是否设置了配置设置SESSION_COOKIE_SECURE。如果您在 localhost 或通过不安全的线路工作,并且您已设置 SESSION_COOKIE_SECURE=True,则不会发送会话 cookie,因此不会发送任何表单、csrf 保护和各种其他操作。在这些情况下请改用SESSION_COOKIE_SECURE=False

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多