W
W
Wolf_Yout2022-04-04 15:50:12
Python
Wolf_Yout, 2022-04-04 15:50:12

Why does redirect fail?

The code:

from flask import Flask, request, render_template, redirect, make_response
app = Flask("app")

@app.route("/")
def _login():
    return render_template("login.html")

@app.route("/login", methods=['get', 'post'])
def _redirector():
    if request.method == "POST":
        user_id = request.form["name"] # Выбераем из формы user_id
        resp = make_response()
        resp.set_cookie('username', user_id)
        return redirect("/chat")
    else:
        return "Error"

@app.route("/chat")
def _chat():
    with open("db/lastauthor.txt", "r") as file:
        author = file.read()
    with open("db/lastmsg.txt", "r") as file:
        content = file.read()
    return render_template("chat.html", author=author, content = content)

@app.route("/send", methods=["get","post"])
def _send():
    if request.method == "POST":
        content = request.form["content"]
    else:
        return "error"
    with open("db/lastmsg.txt", "w") as file:
        file.write(content)
    with open("db/lastauthor.txt", "w") as file:
        file.write(request.cookies.get('username'))
    return render_template("back.html")
    
    

app.run(host="0.0.0.0", port=8080)

When I go to the home site, I enter a nickname, and after pressing Enter, it gives me this:
624ae9643122f487261025.png
(ps After creating the cookie from the documentation, this error started to appear)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YelKa, 2022-04-06
@Wolf_Yout

Turn on debug mode, then flask will describe the error to you right on the page.
It doesn't show the page. an error occurs inside a function _chat()or inside a template (possibly)
app.run(host="0.0.0.0", port=8080, debug=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question