【问题标题】:AssertionError( AssertionError: View function mapping is overwriting an existing endpoint function: conpab 127.0.0.1)AssertionError(AssertionError:视图函数映射正在覆盖现有端点函数:conpab 127.0.0.1)
【发布时间】:2021-06-04 02:59:16
【问题描述】:

有谁知道我为什么使用烧瓶并输入 URL:127.0.0.1/testpge 到 chrome,错误总是显示:AssertionError( AssertionError: View function mapping is overwriting an existing endpoint function: conpab 127.0.0.1.。我的代码在下面:

from flask import *

app=Flask(__name__, template_folder="C:\\Users\\Administrator\\")

@app.route("/testpge", endpoint='testpge')    
def pagee(name):
    con=100090909
    
    if con < 10 :
        @app.route("/conpa", endpoint='conpa')        
        def conpa():
            return "Conpa page is 000" 
    elif con > 10 :
        @app.route("/conpab", endpoint='conpab')
        def conpab():
            return "Conpa page is very big !"
    
    return "Test page 000"

if __name__ =="__main__" :
    app.run(host="127.0.0.1",port="80")

【问题讨论】:

  • 你为什么要把路线放在现有路线中?
  • 我要判断条件。
  • 我可以重定向自己的视图函数吗,像这样,返回redirect(url_for("testpge"))??

标签: python flask endpoint


【解决方案1】:

请检查此解决方案

from flask import *

app=Flask(__name__)

@app.route("/testpge/<string:name>", endpoint='testpge')    
def pagee(name):
    con=100090909
    
    if con < 10 :
        return redirect(url_for("conpa"))
    elif con > 10 :
        return redirect(url_for("conpab"))
    return "Test page 000"

@app.route("/conpa", endpoint='conpa')        
def conpa():
    return "Conpa page is 000" 

@app.route("/conpab", endpoint='conpab')
def conpab():
    return "Conpa page is very big !"

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

网址:http://127.0.0.1:5000/testpge/Akshay

输出:Conpa page is very big !

【讨论】:

  • 它有效,然后请投票并将其标记为已接受
猜你喜欢
  • 2015-05-03
  • 2013-06-19
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 2021-08-08
  • 2019-11-14
  • 2013-11-26
  • 1970-01-01
相关资源
最近更新 更多