Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a "404 Not Found" error even though the link is on the server?

I'm running a simple test site on PythonAnywhere using Flask. When I run the script, the initial site (index.html) appears, and everything seems fine. However, when I click on any of the links (like signup.html), I get a 404 error:

Not Found
The requested URL was not found on the server.
If you entered the URL manually please check your spelling and try again.

However, the HTML files are all in the templates folder, along with index.html. Why can't they be found on the server?

Here is the Python code that runs the app:

from flask import Flask
from flask import render_template

app = Flask(__name__)

@app.route('/')

def runit():
    return render_template('index.html')

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

And here is the HTML portion of index.html that holds the link:

<a class="btn btn-lg btn-success" href="signup.html">Sign up</a>
like image 803
user3047960 Avatar asked Feb 16 '14 05:02

user3047960


1 Answers

You need to create another route for your signup URL, so your main webapp code needs to add a route for '/signup.html', i.e.

from flask import Flask
from flask import render_template

app = Flask(__name__)

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

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

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

If you want your URLs to be a little cleaner, you can do something like this in your Python:

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

And change your link code to match.

<a class="btn btn-lg btn-success" href="signup">Sign up</a>

The main Flask documentation has a good overview of routes in their Quickstart guide: http://flask.pocoo.org/docs/quickstart/

like image 86
Jacinda Avatar answered Oct 14 '22 22:10

Jacinda