Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplainable Flask 404 errors

Tags:

python

flask

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='/',     ) 
like image 533
Jasmijn Avatar asked Jun 26 '14 18:06

Jasmijn


People also ask

What does 404 mean in Python?

404 error means the service is not returning any response. If you can observe between this tag it will return page not found text which means it returns a web page. What you can do is find the "Page not found" text inside the returned webpage. use string.

How do I view 404 errors in Python?

status_code attribute: if r. status_code == 404: # A 404 was issued. If you want requests to raise an exception for error codes (4xx or 5xx), call r.

What is the use of blueprint in flask?

Flask uses a concept of blueprints for making application components and supporting common patterns within an application or across applications. Blueprints can greatly simplify how large applications work and provide a central means for Flask extensions to register operations on applications.


2 Answers

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.

like image 98
Alexis Benoist Avatar answered Oct 02 '22 12:10

Alexis Benoist


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

like image 28
Scott Avatar answered Oct 02 '22 12:10

Scott