Search code examples
pythonflaskweb-deployment

I have started learning flask and when I run my home.html and submit a form then it gives your files can't be accessed. For backend I have used flask


this is my rule.html

<!DOCTYPE html>
<html>
    <head>
        <title>rules</title>
    </head>
    <body>
        <H1>Have fun and enjoy {{name}}</H1>
        <p>So there are some rules that will help you to play this game.</p> 
    </body>
</html>

this one is home page html code. Please help, I saw tutorials but not getting anything.

<!DOCTYPE html>
<html>
    <head>
    <title> Home</title>
    </head>
    <body>
        <h1>Welcome to guess the game!!</h1>
        <form method="post" action="/rule">
            <h3>So, what is your name":</h3>
            <input type="text" name="user">
            <input type="submit" value="submit" >
        </form>
    </body>

this one is my python code.

from flask import Flask,render_template, request, redirect, url_for
app= Flask(__name__)

@app.route('/')
def home():
    return render_template('home.html')


@app.route('/rule', methods=["POST","GET"])
def rule():
    if request.method=="POST":
        user=request.form["user"]
        print(user)
        return redirect(url_for("user",usr=user))
    else:
        return render_template("rule.html")

@app.route("/<usr>")
def user(usr):
    return render_template("rule.html",name=usr)

if __name__ == '__main__':
    app.run(debug=True)

sorry for so many codes. but i need help.

and my os is window 8.1, python -> 3.7.1, flask->1.1.1,werkzeug->1.0.1

error page


Solution

  • When I look at your error page, your browser seems to try to access D:/rule - this should not be served by the file system, but by Flask.

    You need to access your app e.g. as localhost:5000/home.html and then it should work.

    For me it looks like you directly access the html file in your browser from the file system.

    First, you need to run your app, with something like python main.py, where main.py is the file name of your app.

    Then enter the URL, which you see in the console plus append the home.html - it should work then.