Flask View function - rewrite existing endpoint function: type

I recently bought RealPython to learn about Python and web development. However, I came across a road block, which in my opinion is a Python configuration problem on my machine. Any help would be greatly appreciated.

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

Unfortunately, I get this error when I try to run the application:

 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 

Beer freezing gives these requirements as 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 was able to run the code was to change the def type. Do not 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" 

Decision

+6
source share
3 answers

So, you figured out a solution: this is a namespace problem. You have three functions that contradict each other - def type . When you renamed them using different names, this fixed the problem.

By the way, I am the author of Real Python . Correction now.

Hooray!

+14
source

three of your methods have the same name. Shells use the method name to match them.

0
source

You can point multiple cards to the same method:

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

You should not call your method type , as this is the name of an inline type class:

Reference for class type in __builtin__ module:

class type(object)

| type(object) -> the object type

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

0
source

Source: https://habr.com/ru/post/972531/


All Articles