Can't route to "/login" with flask?


When I type /login as url,it will go wrong

For example:

from flask import Flask ,url_for,render_template,request
app = Flask(__name__)

def index():
  return "index"

if __name__== "__main__":

The error turn out to be like this:

Not Found.
The requested URL was not found on the server.

When I replace /login with /login/ or any other words like /log , it will be all right. How does that happen?

2/19/2013 11:47:08 AM

Accepted Answer

Please read the flask quickstart Unique URLs / Redirection Behavior, URL canonicalization and Trailing slash in URLs - which style is preferred?

5/23/2017 11:46:03 AM


You could turn off the strict url mode in the route module, to get the /login/ request working

Add the following code after you app = Flask(__name__) and before you define any routing.

app.url_map.strict_slashes = False

Original Answer

My chrome is messing the request somehow. I open the <F12> developer tools and find that it automatically redirect my /login request to /login/.

Request URL:
Request Method:GET
Status Code:301 MOVED PERMANENTLY (from disk cache)
Remote Address:

Content-Type:text/html; charset=utf-8
Date:Wed, 28 Dec 2016 14:24:44 GMT
Server:Werkzeug/0.11.11 Python/3.5.1

This is awkward. I don't know how to fix this issue. I guess the best solution is to use /login/ style instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow