Flask视图函数映射覆盖现有端点函数:类型

8 投票
3 回答
18328 浏览
提问于 2025-04-18 14:01

我最近购买了RealPython,想学习Python和网页开发。不过,我遇到了一个问题,我觉得可能是我电脑上的Python配置出了问题。希望能得到一些帮助。

我有一个叫做app.py的Flask文档,和RealPython的github上的app.py类似。

# --- Flask Hello World ---#

# import the Flask class from the flask module
from flask import Flask

# create the application object
app = Flask(__name__)

# use decorators to link the function to a url
@app.route("/")
@app.route("/hello")

# define the view using a function, which returns a string
def hello_world():
    return "Hello, World!"

# dynamic route
@app.route("/test/<search_query>")
def search(search_query):
    return search_query

# dynamic route with an int type
@app.route("/integer/<int:value>")
def type(value):
    print value + 1
    return "correct"

# dynamic route with an float type
@app.route("/float/<float:value>")
def type(value):
    print value + 1
    return "correct"

# dynamic route that accepts slashes
@app.route("/path/<path:value>")
def type(value):
    print value
    return "correct"



# start the development server using the run() method
if __name__ == "__main__":
    app.run()

不幸的是,当我尝试运行这个应用时,出现了这个错误:

machine:flask-hello-world machine$ source env/bin/activate
(env)machine:flask-hello-world machine$ python app.py
Traceback (most recent call last):
  File "app.py", line 29, in <module>
    @app.route("/float/<float:value>")
  File "/Volumes/disk2/Home/Library/RealPython/flask-hello-world/env/lib/python2.7/site-packages/flask/app.py", line 1013, in decorator
    self.add_url_rule(rule, endpoint, f, **options)
  File "/Volumes/disk2/Home/Library/RealPython/flask-hello-world/env/lib/python2.7/site-packages/flask/app.py", line 62, in wrapper_func
    return f(self, *args, **kwargs)
  File "/Volumes/disk2/Home/Library/RealPython/flask-hello-world/env/lib/python2.7/site-packages/flask/app.py", line 984, in add_url_rule
    'existing endpoint function: %s' % endpoint)
AssertionError: View function mapping is overwriting an existing endpoint function: type

Pip freeze显示这些是虚拟环境env的依赖项。现在安装的是Python 2.7。

Flask==0.10.1
Jinja2==2.7.3
MarkupSafe==0.23
Werkzeug==0.9.6
itsdangerous==0.24
wsgiref==0.1.2

我唯一能让代码运行的方法是更改def类型。不过其实不应该这样做……

# dynamic route with an int type
@app.route("/integer/<int:value>")
def type(value):
    print value + 1
    return "correct"

# dynamic route with an float type
# change to type1 so dev server will spool up
@app.route("/float/<float:value>")
def type1(value):
    print value + 1
    return "correct"

# dynamic route that accepts slashes
# change to type2 so dev server will spool up
@app.route("/path/<path:value>")
def type2(value):
    print value
    return "correct"

解决方案

3 个回答

2

你可以让多个映射指向同一个方法:

@app.route("/integer/<int:value>")
@app.route("/float/<float:value>")
def var_type(value):
    print value + 1
    return "correct"

不要把你的方法命名为 type,因为这个名字已经被内置的类型类使用了:

关于模块 __builtin__ 中的类 type 的帮助信息:

class type(object)

| type(object) -> 返回对象的类型

| type(name, bases, dict) -> 创建一个新类型

2

你有三个方法的名字是一样的。这些包装器是通过方法的名字来进行匹配的。

21

所以,你找到了问题的解决办法:这是一个命名空间的问题。你有三个函数名字冲突了,都是 def type。当你把它们改成不同的名字后,这个问题就解决了。

顺便说一下,我是 Real Python 的作者。现在来纠正一下。

谢谢!

撰写回答