flask cannot access /login

A problem

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

When learning flask, you need to define the login page. The routing function is naturally defined as follows:

@app.route("/login", methods=['GET', 'POST']) 

After writing the routing function, I visited the page and found that it was:

The source code is as follows:

@app.route("/login", methods=['GET', 'POST'])
def login():
    return render_template('login.html', title='Sign In')

Two solution ideas

Try to solve the problem on Baidu, unfortunately, the search ability is limited and I can't find it. Try to think about whether this route and view function is not added to the url_map, and then try to print the output:

if __name__ == '__main__':
    # 通过url_map可以查看整个flask中的路由信息
    print(app.url_map)
    # 启动flask程序
    app.run(debug=True)

The results are as follows:

No problem, it has, but it can't be accessed. When you want to give up, gg

The result is really found. Change "/login" in @app.route("/login", methods=['GET','POST']) to "/login/" or "/loginxxx"

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?

Three further thinking, why?

 

Why is'/index' OK, but'/login' cannot be accessed?

 

The explanation in the flask document is:

The canonical URL has a slash. It is similar to a folder or directory in a file system. If the URL you visit does not have a slash, Flask will redirect you to the canonical URL with a slash (the reason why /login cannot be accessed).

About routing URL without slashes. It is similar to the path name of a file. Using a slash to access the URL will generate a 404 "not found" error. This helps keep the URLs of these resources unique, which helps search engines avoid indexing the same page twice.

However, I don't think the explanation is enough, so why can /index or /hello be accessed? as follows:

@app.route('/index')
def index():
    return 'Index Page'

@app.route('/hello')
def hello():
    return 'Hello, World'

Read the opinions of different people:

 

I feel that the explanation is far-fetched, but I still failed to answer the second question

 

Understand by yourself

'/login' is the built-in routing,

Need to be different when writing by yourself,

So we can use'/login2','/login3','/login4','/login/ to access successfully

 

Grasp the rules, and connect to the special'/login' is a built-in route. When encountering similar situations in the future, slowly join this special pool.

 

Colleagues with ideas are welcome to discuss below, (the main substance is what happened, I didn’t understand it, only the problem can be solved)

 

Guess you like

Origin blog.csdn.net/qq_39463175/article/details/110731109