Trailing slash triggers 404 in Flask path rule
Your /users
route is missing a trailing slash, which Werkzeug interprets as an explicit rule to not match a trailing slash. Either add the trailing slash, and Werkzeug will redirect if the url doesn't have it, or set strict_slashes=False
on the route and Werkzeug will match the rule with or without the slash.
@app.route('/users/')
@app.route('/users/<path:path>')
def users(path=None):
return str(path)
c = app.test_client()
print(c.get('/users')) # 302 MOVED PERMANENTLY (to /users/)
print(c.get('/users/')) # 200 OK
print(c.get('/users/test')) # 200 OK
@app.route('/users', strict_slashes=False)
@app.route('/users/<path:path>')
def users(path=None):
return str(path)
c = app.test_client()
print(c.get('/users')) # 200 OK
print(c.get('/users/')) # 200 OK
print(c.get('/users/test')) # 200 OK
You can also set strict_slashes
for all URLs.
app.url_map.strict_slashes = False
However, you should avoid disabling strict slashes in most cases. The docs explain why:
This behavior allows relative URLs to continue working even if the trailing slash is omitted, consistent with how Apache and other servers work. Also, the URLs will stay unique, which helps search engines avoid indexing the same page twice.
Flask adds trailing slash to URL, no routes match
Your WSGIScriptAlias is misconfigured. Your app is being served by Apache relative to /foo
To get the behavior you want, the alias should be for the root directory, I.E. /
instead of /foo
Trailing slash in Flask route
You are on the right tracking with using strict_slashes
, which you can configure on the Flask app itself. This will set the strict_slashes
flag to False
for every route that is created
app = Flask('my_app')
app.url_map.strict_slashes = False
Then you can use before_request
to detect the trailing /
for a redirect. Using before_request
will allow you to not require special logic to be applied to each route individually
@app.before_request
def clear_trailing():
from flask import redirect, request
rp = request.path
if rp != '/' and rp.endswith('/'):
return redirect(rp[:-1])
Why does putting the word Admin in the route of a flask application cause the page to 404?
It's not the n letter that's causing the error, but the /
at the end of the URL.
example.com/tableAdmin
and example.com/tableAdmin/
are different URLs in this context. You need to handle them separately or add to the same handler.
You can do this:
@application.route('/console/tableAdmin')
@application.route('/console/tableAdmin/')
def editTable():
return flask.render_template('formTest.html')
and the URL will become slash-agnostic. If you want to make all your URL slash-agnostic, you need to handle and remove the slashes in @app.before_request
.
See this question for more details.
Related Topics
Comparing Two .Txt Files Using Difflib in Python
Calling Matlab Functions from Python
How to Configure Atom to Run Python3 Scripts
Get Timezone from City in Python/Django
Getting the Index of a Row in a Pandas Apply Function
Python - Download Images from Google Image Search
Error "Importerror: Dll Load Failed: %1 Is Not a Valid Win32 Application"
How to Timeout Function in Python, Timeout Less Than a Second
Parsing .Properties File in Python
Unicodeencodeerror: 'Latin-1' Codec Can't Encode Character
Python Requests Ssl Error - Certificate Verify Failed
Converting "Yield From" Statement to Python 2.7 Code
How to Sort and Remove Duplicates from Python List