Flask的一条路线下是否可以有两个不同的动作/按钮?

时间:2019-10-16 07:30:39

标签: python post button flask

我正在尝试创建一个简单的Web应用程序,在该应用程序中,用户上载两个文档,并且对它们的处理方式有两种选择:找到词汇差异或语义相似性。 “差异”按钮可以正常工作,但我无法使“相似性”按钮正常工作,甚至似乎也无法接受。

我尝试弄乱if语句以查看是否有任何更改很重要,但是目前我的逻辑没有任何问题。但是,我对此肯定是错误的

routes.py

#snippet of code with relevant function
@app.route("/uploading", methods=["GET","POST"])
def uploading():
    if request.method =="POST":
        file1 = request.files['file1']
        file2 = request.files['file2']
        if request.form['differences']:
            result = dif(file1.filename,file2.filename)
            item = serve_pil_image(result)
            return item
        if request.form['similarity']:
            doc = (file1.filename).read()
            doc = gensim.utils.simple_preprocess(doc)
            doc2 = (file2.filename).read()
            doc2 = gensim.utils.simple_preprocess(doc2)
            new_vector1 = model.infer_vector(doc)
            new_vector2 = model.infer_vector(doc2)
            dot = np.dot(new_vector1, new_vector2)
            norma = np.linalg.norm(new_vector1)
            normb = np.linalg.norm(new_vector2)
            cos = dot / (norma * normb)
            return 'The similarity score between file one and file two is: ', str(cos)

 return render_template('upload.html')

upload.html

<html>
    <head>
        <title>Simple file upload using Python Flask</title>
    </head>
    <body>
        <form action="{{ url_for('uploading') }}" method="post" enctype="multipart/form-data">
            Choose the first file: <input type="file" name="file1" value="file one"/><BR>
            Choose the second file: <input type="file" name="file2" value="file two"/><BR>
            <input type="submit" name="differences" value="Find Differences">
            <input type="submit" name="similarity" value="Similarity Score (Percentage)">
        </form>
</html>

编辑

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/sozube/PycharmProjects/pdf-diff/app/routes.py", line 108, in uploading
    if request.form['differences']:
  File "/usr/local/lib/python3.6/dist-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
KeyError: 'differences'

我收到错误消息werkzeug.exceptions.BadRequestKeyError,其中包含KeyError:'differences'

1 个答案:

答案 0 :(得分:0)

您需要处理两个键之一不会出现的问题。

if request.form.get('differences'):
    # the body of if block here
if request.form.get('similarity'):
    # the body of if block here

或者

if request.form.get('differences'):
    # the body of if block here
else:
    # the body of else block here