TypeError: 视图函数未返回有效响应。Flask

huangapple go评论62阅读模式
英文:

TypeError: The view function did not return a valid response. Flask

问题

所以,我正在制作一个博客网站。它一直给我显示错误:

TypeError: 'createblog' 的视图函数没有返回有效的响应。该函数要么返回了 None,要么没有带有 return 语句结束。

但问题出在我的 Python 代码中,我的 createblogs 路由结构如下:

@app.route("/createblog", methods=["POST"])
def createblog():
title = request.args.get("title")
description = request.args.get("description")
content = request.args.get("content")
username = request.args.get("username")
password = request.args.get("password")

if not title or not description or not content or not username or not password:
    return

cursor.execute("INSERT INTO blogs (poster_id, title, description, content) VALUES ((SELECT id FROM users WHERE username=? AND password=?), ?, ?, ?)", (username, password, title, description, content))
connection.commit()

return jsonify(status=200) 
正如你所看到的,我有 `return jsonify(status=200)`。让这更奇怪的是,我在登录时使用了相同的技巧,而它却奏效了!

@app.route("/register", methods=["POST"])
def register():
username = request.form.get("username")
password = request.form.get("password")

print(username, password)

if not username or not password:
    return

cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
connection.commit()

return jsonify(status=200)
还有我的 JavaScript AJAX 代码:

const xhttp = new XMLHttpRequest();
const postObject = {
username: username.value,
password: password.value,
title: title.value,
description: description.value,
content: content.value,
}
const post = JSON.stringify(postObject);
xhttp.open("POST", "/createblog", true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(post);

我已经排除了所有其他可能的原因(例如 HTML),与这个问题没有相关性。非常感谢你的帮助!

我尝试重新启动开发服务器,希望能改变一些东西,但没有!这个问题仍然存在。然后我试图在 StackOverflow 上发布一个询问,希望能在这个隧道(在这个比喻中代表我的问题)的尽头找到答案。
英文:

So, I am making a website to make blogs. It keeps on giving me the error:

TypeError: The view function for 'createblog' did not return a valid response. The function either returned None or ended without a return statement.

But the problem is in my python code, my createblogs route is structured as so:

@app.route("/createblog", methods=["POST"])
def createblog():
    title = request.args.get("title")
    description = request.args.get("description")
    content = request.args.get("content")
    username = request.args.get("username")
    password = request.args.get("password")

    if not title or not description or not content or not username or not password:
        return

    cursor.execute("INSERT INTO blogs (poster_id, title, description, content) VALUES ((SELECT id FROM users WHERE username=? AND password=?), ?, ?, ?)", (username, password, title, description, content))
    connection.commit()

    return jsonify(status=200) 

As you can see, I have return jsonify(status=200). What makes this really weird is I used the same trick for logging in and it worked!

@app.route("/register", methods=["POST"])
def register():
    username = request.form.get("username")
    password = request.form.get("password")

    print(username, password)

    if not username or not password:
        return

    cursor.execute(
        "INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
    connection.commit()

    return jsonify(status=200)

And my javascript AJAX code:

   const xhttp = new XMLHttpRequest();
            const postObject = {
                username: username.value,
                password: password.value,
                title: title.value,
                description: description.value,
                content: content.value,
            }
            const post = JSON.stringify(postObject);
            xhttp.open("POST", "/createblog", true);
            xhttp.setRequestHeader("Content-Type", "application/json");
            xhttp.send(post);

I've eliminated all other causes (e.g. HTML) as no correlation to this issue. Help much appreciated!

I tried to restart my development server, in hope that that would change something, but no! This issue persisted. Then I'm trying to post an inquiry on stackoverflow, in hopes of light at the tunnel which in this metaphor is the explanation to my given asking.

答案1

得分: 1

你有尝试在你的createblog函数中打印用户名、密码、标题、描述和内容吗?正如错误所述,你正在返回None,这意味着你的某个条件不满足。为了解决这个问题,在你的AJAX请求中,你可以进行一些验证,以确保任何字段的.value都不是null。你也可以在后端进行验证。希望这有所帮助。

对于未来的请求,包括发送到后端的样本数据将会很有帮助。否则很难重现错误并提供帮助。

英文:

Have you tried printing username, password, title, description, content in your createblog function? As the error states, you are returning None, which means that one of your not conditions is being triggered. To solve this, in your AJAX request, you can do some validation to ensure that the .value for any of the fields is not null. You could do validation in the backend as well. Hope this helps.

For future requests, it would be helpful to include additional information like the sample payload being sent to your backend. Hard to reproduce the error and help otherwise.

huangapple
  • 本文由 发表于 2023年5月14日 11:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定