Unexplainable Flask 404 errors
Asked Answered
R

9

47

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:

  • Locally, the site still works, only on my VPS it shows this strange behaviour.
  • url_for works and app.view_functions contains all the routes as well.
  • And yet, I keep getting 404s on the VPS, even for / and anything under /static/.

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='/',
    )
Rigel answered 26/6, 2014 at 18:2 Comment(7)
have you had a look at the server logs? there should be some hint about what's going wrong there.Colic
I have. Unfortunately, they show nothing out of the ordinary (except that the status code of every access is 404).Rigel
that's probably the access log, you need to look at the error log.Colic
Yes, that was the access log. However, there weren't any errors at all in the error logs (none relevant, that is: some notices about Apache restarting and the like, but nothing that happened during recent requests)Rigel
How are you deploying your flask application in the VPS? Are you using uwsgi or gunicorn? Look in your wsgi server log, not the apache logs. Although if you are deploying with mod_wsgi in Apache, the errors should be in the apache error log. Are there separate error logs for each virtual host in Apache? If so, verify you're looking at the correct error log for your virtual host. Also, has the apache/wsgi configuration changed recently?Chapnick
mod_wsgi. And I am, because the error log does show errors in the past (as well as errors I made when trying to fix things), and I haven't changed any configuration at all. And the problem doesn't seem to be the Flask installation either, as I've upgraded Flask and the problem remained.Rigel
I have the same problem, I asked in an issueMorril
M
62

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.

Morril answered 11/8, 2015 at 15:1 Comment(8)
SAVED ME! I had added it after I had a real URL, it was not working, even though it did match the URL. Removed it, and it started working. Thank you.Candida
I had the same behavior, 404s everywhere until I removed SERVER_NAME from the config. However, I don't understand why that actually creates this situation. Is there any explanation or documentation of this behavior?Informed
i have a brand new install of flask on python 3.6 pip3 18.1. i have not touched anything other than create a new app.py with 3 line of code creating a new app. it starts fine but get the 404 when i use the url from consoleTeel
when i print it, it returns "None" print('server name ', app.config["SERVER_NAME"])Teel
Lol, I spent wayyyyyy too long trying to figure this out and a single '#' fixed it. ThanksAlmanac
This also fixed my problem. Had started requiring use of (a correctly specified) "SERVER_NAME" param within my app in development, but hadn't specified it on the Staging instance.Murder
It is possible to test this with pytest?Denunciation
For anyone who is still having trouble, if you have hosted on localhost (e.g. http://127.0.0.1:5000/), make sure your URL points to the correct @app.route() name/function. If you have a function def index and route @app.route("/index"), make sure you visit http://127.0.0.1:5000/indexTrothplight
D
23

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.

Desex answered 9/7, 2019 at 7:47 Comment(1)
Thanks! I lost 45 minutes on this thing! :)Clearance
P
15

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).

Peptic answered 29/12, 2014 at 2:1 Comment(1)
Note: I had to add from app import views to the last line of in my app/__init__.pyIaniana
S
2

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

Slovakia answered 9/5, 2018 at 18:11 Comment(0)
P
2

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') 
Popsicle answered 14/6, 2018 at 3:2 Comment(0)
S
1

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.

Sorites answered 11/8, 2020 at 7:15 Comment(0)
U
0

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>
Ultima answered 31/7, 2020 at 20:20 Comment(0)
C
0

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'
Christy answered 6/6, 2022 at 18:23 Comment(0)
Q
0

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.

Quechua answered 8/7 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.