Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask View function mapping is overwriting an existing endpoint function: type

Tags:

python

flask

I recently purchased RealPython to learn about Python and web development. However, I have ran into a road block that I think is a Python configuration issue on my machine. Any help would be much obliged.

So I have a Flask document called app.py similar to 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()

I unfortunately receive this error when trying to run the app:

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 gives these as requirements for virtualenv env. Python 2.7 is what is installed.

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

The only way I've been able to get the code to run is by changing def type. One should not have to do this though...

# 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

like image 601
atrueresistance Avatar asked Jul 20 '14 14:07

atrueresistance


2 Answers

So, you figured out the solution: It's a namespace issue. You have three functions that are conflicting against one another - def type. When you renamed them using different names, this fixed the issue.

I am the author of Real Python, by the way. Correcting now.

Cheers!

like image 183
Michael Avatar answered Nov 15 '22 20:11

Michael


three of your methods have the same name. The wrappers use the name of the method to do their mapping

like image 43
corvid Avatar answered Nov 15 '22 22:11

corvid