Jasmijn
Jasmijn

Reputation: 10477

Unexplainable Flask 404 errors

I have a website that uses Flask. It used to work well, but since recently, every request returns a 404, and it seems it can't find the right endpoints. However:

Here's part of the code, it's a bit much to show all of it and it's not all relevant:

#snip

from flask import Flask, render_template, abort, request, redirect, url_for, session
from flask.ext.babelex import Babel
from flask.ext import babelex

#snip

app = Flask(__name__)
app.secret_key = #snip

#snip

#just one of the routes
@app.route('/')
def about():
    return render_template('about.html')

#snip

@app.errorhandler(404)
def page_not_found(e):
    #snip
    return render_template('404.html'), 404

#snip

if __name__ == '__main__':
    app.run(debug=True)
else:
    app.config.update(
        SERVER_NAME='snip.snip.com:80',
        APPLICATION_ROOT='/',
    )

Upvotes: 47

Views: 75741

Answers (9)

Alex
Alex

Reputation: 2239

Probably it will be useful for someone. Once, I had two projects open at the same time and accidentally ran the wrong one. So, if you read this thread, try running 'pwd' to check which folder you are currently using.

Upvotes: 0

gene b.
gene b.

Reputation: 12034

For me, I was getting 404s on every request due to missing favicon.ico and manifest.json. The solution was to provide the missing favicon.ico, and remove the link rel refence to manifest.json.

I found this out by printing the path that was causing problems on every request in my @app.errorhandler(Exception):

from flask import request

@app.errorhandler(Exception)
def handle_exception(err):
  path = request.path # this var was shown to be 'favicon.ico' or 'manifest.json'

Upvotes: 0

pvilas
pvilas

Reputation: 1417

Also check carefully the routes. If some does not end with a slash but you are calling it with a trailing slash, flask will return a 404.

I had this error following a url from an email client that (I don't know why) append a trailing slash to the urls.

See documentation.

Upvotes: 1

Daniel
Daniel

Reputation: 81

I had this problem and in my case it was about my templates. I had an index page like this:

<div>
    <a href="index.html">Home</a> |
    <a href="login.html">Login</a> |
    <a href="register.html">Register</a>
    <hr>
</div>

I should have used url_for('index') instead of index.html.

<div>
    <a href="{{ url_for('index') }}">Home</a> |
    <a href="{{ url_for('login') }}">Login</a> |
    <a href="{{ url_for('register') }}">Register</a>
    <hr>
</div>

Upvotes: 0

Alveona
Alveona

Reputation: 978

Had the same issue because the following lines

if __name__ == '__main__':
    app.run(host='127.0.0.1', threaded=True)

were declared before the function and decorator.

Moving these lines in the very bottom of file helped me.

Upvotes: 23

Braden Holt
Braden Holt

Reputation: 1594

I received this error from defining my flask_restful route inside the create_app method. I still don't quite understand why it didn't work but once I changed the scope / moved it outside as shown below it worked.

from flask import Flask
from flask_restful import Resource
from extensions import db, api
from users.resources import User

def create_app():
    app = Flask(__name__)
    app.config.from_object('settings')
    db.init_app(app)
    api.init_app(app)

    return app

api.add_resource(User, '/users') 

Upvotes: 2

user7804097
user7804097

Reputation: 308

Extremely old by this point, but mine was related to bug in importing. I'm using blueprints and I had:

from app.auth import bp

instead of

from app.main import bp

I was importing the wrong bp/ routes

Upvotes: 2

Alexis Benoist
Alexis Benoist

Reputation: 2410

I had the same issue. I had it because I changed the parameters SERVER_NAMEof the config to a name which is not the name of the server.

You can solve this bug by removing SERVER_NAME from the config if you have it.

Upvotes: 62

Scott
Scott

Reputation: 3344

I know this is old, but I just ran into this same problem. Yours could be any number of issues but mine was that I had commented out the from app import views line in my __init__.py file. It gave me the same symptom: every endpoint that required @app.route and a view was responding with 404's. Static routes (.js and .css) were fine (that was my clue).

Upvotes: 15

Related Questions