【问题标题】:I kept getting TypeError: can only concatenate str (not "NoneType") to str我不断收到 TypeError: can only concatenate str (not "NoneType") to str
【发布时间】:2020-08-29 22:21:50
【问题描述】:

我一直在尝试运行此代码,但一直收到类型错误,我搜索了网络但没有找到解决问题的方法。 注意:这是整个代码的一小部分

@app.route("/search", methods = ["GET", "POST"])
def search():
    if "user_email" not in session:
        return render_template("sign.html", error="Please Login First", work="Failed")

    if request.method == 'GET':
        title = request.form.get('title')
        isbn = request.form.get('isbn')
        author = request.form.get('author')     
        searchs = db.execute("SELECT * FROM books WHERE author iLIKE '%"+author+"%' OR title iLIKE '%"+title+"%' OR isbn iLIKE '%"+isbn+"%'").fetchall()
        return render_template('search.html', work = 'Success', searchs = searchs )

【问题讨论】:

  • 如果密钥不在表单中,request.form.get 将返回 None。所以你需要处理表单中没有“title”、“isbn”或“author”的情况。您的查询假定它们都存在。
  • @snakecharmerb 这个案子怎么办,我是初学者
  • 阅读文档,了解如何在 Python 中编写条件和处理异常

标签: python python-3.x postgresql flask psql


【解决方案1】:

作为初学者,一个相当简单的问题解决方案是对每个变量运行单独的查询,如果它们被提供,并将它们合并到模板的单个列表中(我们可以逃脱因为我们总是查询同一个表,所以输出的行总是有相同的结构)。

@app.route("/search", methods = ["GET", "POST"])
def search():
    if "user_email" not in session:
        return render_template("sign.html", error="Please Login First", work="Failed")

    if request.method == 'GET':
        # Make a list to hold the results.
        searches = []
        title = request.form.get('title')
        if title is not None:
            titles = db.execute("SELECT * FROM books WHERE title ILIKE %s", ('%'+title+'%',)).fetchall()
            searchs.extend(titles)
        isbn = request.form.get('isbn')
        if isbn is not None:
            isbns = db.execute("SELECT * FROM books WHERE isbn ILIKE %s", ('%'+isbn+'%',)).fetchall()
            searchs.extend(isbns)
        author = request.form.get('author')    
        if author is not None:
            authors = db.execute("SELECT * FROM books WHERE author ILIKE %s", ('%'+author+'%',)).fetchall()
            searchs.extend(authors) 
        return render_template('search.html', work = 'Success', searchs = searchs )

有更好的方法可以做到这一点,但现在应该可以了。注意查询的形式

result = db.execute("SELECT thing FROM table WHERE thing = %s", (value,))

确保查询值以其期望的格式发送到数据库(在行话中它们被“引用”),并防止 SQL 注入攻击,当恶意用户将 SQL 脚本输入到您的 Web 表单中以查看是否他们可以从您的数据库中下载数据,或者更改或删除其中包含的数据。

【讨论】:

    【解决方案2】:

    我假设您收到此错误是因为 request.form.get() 返回 None 值。此外,request.form 用于检索 POST 数据,但您似乎将其用于 GET 请求。

    过去,我使用此命令从 GET 请求中获取参数

    search = request.args.get("search_query", "")
    

    request.args.get 中的第二个参数是默认值

    .fetchall() 是不需要的,可能是原因,你应该使用 db.engine.execute()

    query = ('SELECT State_name, count(State_name) FROM "University" group by State_name')
    
    res = db.engine.execute(query)
    

    【讨论】:

    • 谢谢,现在可以使用了。但现在搜索返回我数据库中的所有数据
    • 我添加到答案中
    猜你喜欢
    • 2021-08-31
    • 1970-01-01
    • 2022-11-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2019-02-02
    • 2020-02-29
    相关资源
    最近更新 更多