Home > other >  My html link is returning a 404 not found error (Rendered template)
My html link is returning a 404 not found error (Rendered template)

Time:09-15

I used python to render a very simple html page. In the html page, I made a link that is supposed to send you to another page. The first html page works, however, when I click on the link it sends me to a 404 page not found error. My index.html and about.html are in a folder called templates. Here is how it looks: enter image description here Here is my code:

app.py:

from flask import Flask, render_template

app = Flask(__name__)
@app.route('/')
def home():
    return render_template('index.html')

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

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Don's first website :D">
    <title>Don's website</title>
    <style>
    </style>
</head>
<body>
    <a href="/templates/contact.html">other website</a>
</body>
</html>

contact.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello</title>
</head>
<body>
    <h1>please work!</h1>
</body>
</html>

CodePudding user response:

You need to create a route for each template.

app = Flask(__name__)
@app.route('/')
def home():
    return render_template('index.html')

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



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

replace your href in your contact template to the following

<a href="{{ url_for('contact') }}">
  • Related