0

I am new to flask and python and I was trying to use the render template function open an URL. the 404 error keeps showing instead of the HTML that is in the templates folder.

My folder structure is as follows:

coding folder/

main.py

templates/

----profile.html

The code in my main.py file is as follows:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/profile/<name>")

def profile(name):

    return render_template("profile.html", name=name)


if __name__ == "__main__":

     app.run()

my profile.html file contains the following code:

<!doctype html>

<title>sambit's page</title>

<h3>Wassup {{ name }} </h3>

I have been scratching my head over this for more than 4 hours. Please Help!!!!

1
  • 1
    Could you say, what request is being shown in the shell against the 404 status code? Commented Sep 24, 2017 at 13:30

1 Answer 1

5

Are you trying to access using /profile/myname/ or /profile/myname ?

Since your route definition doesn't have a trailing slash, Flask will throw a 404 if you access that route with a trailing slash in your browser. Read more here: Doc

Though they look rather similar, they differ in their use of the trailing slash in the URL definition. In the first case, the canonical URL for the projects endpoint has a trailing slash. In that sense, it is similar to a folder on a filesystem. Accessing it without a trailing slash will cause Flask to redirect to the canonical URL with the trailing slash. In the second case, however, the URL is defined without a trailing slash, rather like the pathname of a file on UNIX-like systems. Accessing the URL with a trailing slash will produce a 404 “Not Found” error.

Sign up to request clarification or add additional context in comments.

3 Comments

I got it, thanks a lot @Sreenadh! It is working fine now after I removed the slash from the URL.
@sambit No problem. Glad to help :)
Could you please mark the answer as it seems to have solved your problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.