简体   繁体   English

烧瓶 - 400 错误请求

[英]Flask - 400 Bad request

I have this python code which should take in data from an html form and use it in a WHERE clause:我有这个 python 代码,它应该从 html 表单中获取数据并在 WHERE 子句中使用它:

@app.route('/search', methods=['GET'])
def search():
    connect = cx_Oracle.connect("benjamin", "siliyetu", "PRINCE-PC/XE")
    cursor = connect.cursor()

    searched = request.form['search']
    named_params = {'search':searched}
    query = cursor.execute("SELECT * FROM edited WHERE REGEXP_LIKE (cod_ed, 
                            :search) OR REGEXP_LIKE (nome_ed,:search) OR 
                            REGEXP_LIKE (endereco,:search) OR REGEXP_LIKE 
                            (telefone,:search) OR REGEXP_LIKE 
                            (cidade,:search)", named_params)

    results = cursor.fetchall()
    posts = list(results)
    return render_template('search.html', posts=posts)

and the template I'm using is this(part of the template anyway. Its not the whole thing):我正在使用的模板是这个(无论如何都是模板的一部分。它不是全部):

<form method="POST" action="/editora" class="form-outline" >
        <div class="col-lg-7 col-offset-6 right">
            <div class="form-group mx-lg-3 mb-2">
                <label for="element-7" ></label>
                <input id="search" name="search" type="text" class="form-control" placeholder="Pesquisar..." />
                <label></label>
                <a class="btn btn-primary " type="submit" href="search">Pesquisa</a>

When I try to use the data from the form, it gives me a当我尝试使用表单中的数据时,它给了我一个

werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'search'

But when I input data using input() it works fine.但是当我使用 input() 输入数据时,它工作正常。 What gives!?是什么赋予了!?

How would I go about fixing this issue?我将如何解决这个问题? I also want to add some regular expressions in the where clause but its not budging.我还想在 where 子句中添加一些正则表达式,但不要让步。 How do I do that too?我也怎么做?

Ps- I'm working with oracle express edition 11g Ps-我正在使用 oracle express edition 11g

Without having a traceback (you are running with the debug server while developing, right?), the exception you're getting comes from the而不必回溯(你调试服务器上运行,同时开发,对吧?),你得到的例外来自

searched = request.form['search']

line.线。

  • the HTML example you have POSTs to /editora你有 POST 到/editora的 HTML 示例
  • the Python code you have has a route /search (not /editora ) and the view won't accept POST requests anyway ( methods=['GET'] ).您拥有的 Python 代码有一个路由/search (不是/editora )并且该视图无论如何都不会接受 POST 请求( methods=['GET'] )。

Are you sure the snippets you've posted are correct?你确定你发布的片段是正确的吗?

request.form is only populated for POST requests, anyway. request.form只为 POST 请求填充,无论如何。

If you want to submit data to search route, you form action should point to that route.如果你想提交数据到搜索路线,你的表单动作应该指向该路线。

<form method="POST" action="/search" class="form-outline" >

And if you want for a search route to get that data from POST request, you should put in methods 'POST' value.如果你想要一个搜索路径从 POST 请求中获取该数据,你应该输入方法 'POST' 值。

@app.route('/search', methods=['GET', 'POST'])

The reason why you get:你得到的原因:

werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'search'
  1. because you are sending something to the application or server that they cannot handle.因为您正在向应用程序或服务器发送他们无法处理的内容。 ( http://werkzeug.pocoo.org/docs/0.14/exceptions/#werkzeug.exceptions.BadRequest ) http://werkzeug.pocoo.org/docs/0.14/exceptions/#werkzeug.exceptions.BadRequest
  2. KeyError is for trying to access dict object parameter 'search' which doesn't exist beucase form is never submited. KeyError 用于尝试访问不存在的字典对象参数“搜索”,因为从未提交过表单。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM