Flask视图函数映射正在覆盖现有的终结点函数:typ

2024-05-19 00:00:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近购买了RealPython来学习Python和web开发。但是,我遇到了一个路障,我认为这是机器上的Python配置问题。任何帮助都将不胜感激。

所以我有一个名为app.py的Flask文档,类似于RealPython's 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将这些作为virtualenvenv的要求。Python2.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"

Solution


Tags: thepyappflaskhelloworldreturnvalue
3条回答

所以,您找到了解决方案:这是一个名称空间问题。您有三个相互冲突的函数-def type。当您使用不同的名称重命名它们时,这解决了问题。

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

干杯!

你的三种方法同名。包装器使用方法的名称来进行映射

可以让多个贴图指向同一方法:

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

不应将方法命名为type,因为它是内置类型类的名称:

Help on class type in module __builtin__:

class type(object)

| type(object) -> the object's type

| type(name, bases, dict) -> a new type

相关问题 更多 >

    热门问题