pythonsqliteflaskweb-development-serversqlite3-python

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


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.


Solution

  • 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.