5

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

For example:

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

@app.route('/login')
def index():
  return "index"

if __name__== "__main__":
  app.run()

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?

Nazik
  • 8,696
  • 27
  • 77
  • 123
winoi
  • 237
  • 2
  • 4
  • 11
  • 1
    running your example and accessing http://127.0.0.1:5000/login works just fine for me... what shows on your console? – pcalcao Feb 19 '13 at 11:47
  • "Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again." – winoi Feb 19 '13 at 12:03
  • 1
    That's what appears on your browser, right? How about the log? something like: `127.0.0.1 - - [19/Feb/2013 11:46:47] "GET /login HTTP/1.1" 200 -` – pcalcao Feb 19 '13 at 12:04
  • 127.0.0.1 - - [19/Feb/2013 20:11:21] "GET /login/ HTTP/1.1" 404 - "/" after "login" is automatically added. It's just after "login" that "/" will be added.I don't know why. – winoi Feb 19 '13 at 12:11

2 Answers2

7

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

Community
  • 1
  • 1
Joe
  • 6,460
  • 1
  • 19
  • 15
  • But why it automatically replace "/login" with "/login/"? And then the browser will not find the page. – winoi Feb 19 '13 at 14:11
  • Please read flask quickstart Unique URLs / Redirection Behavior part, it has explained the reason why flask do this way. – Joe Feb 19 '13 at 14:18
  • For you given code, I can access `http://127.0.0.1:5000/login`,it will not redirect to `http://127.0.0.1:5000/login/`; and when I try to acess `http://127.0.0.1:5000/login/`, it report 404 error. It is in line with Unique URLs / Redirection Behavior. – Joe Feb 19 '13 at 14:40
  • It's all right now. "http://127.0.0.1:5000/login/" was recorded in the browser.So when I type"http://127.0.0.1:5000/login" , it will add "/". It's the brower that troubles me. Thank you for your help. – winoi Feb 19 '13 at 15:45
0

EDIT

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

General
Request URL:http://roxma.org:8000/hello
Request Method:GET
Status Code:301 MOVED PERMANENTLY (from disk cache)
Remote Address:127.0.0.1:1080

Request
Content-Length:263
Content-Type:text/html; charset=utf-8
Date:Wed, 28 Dec 2016 14:24:44 GMT
Location:http://roxma.org:8000/hello/
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.

rox
  • 525
  • 7
  • 16