-2

Below is my flask code and it's working fine

from flask import Flask

app = Flask(__name__)

@app.route("/")
@app.route("/hello")
def hello_world():
    return "Hello, World!"

@app.route("/test")
def search():
    return "Hello"

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

but when I am adding the this code

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

it's giving the following error

Traceback (most recent call last): File "app.py", line 19, in def search(search_query): File "C:\Users\khadush\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1315, in decorator self.add_url_rule(rule, endpoint, f, **options) File "C:\Users\khadush\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 98, in wrapper_func return f(self, *args, **kwargs) File "C:\Users\khadush\AppData\Local\Programs\Python\Python38-32\lib\site-packages\flask\app.py", line 1282, in add_url_rule raise AssertionError( AssertionError: View function mapping is overwriting an existing endpoint function: search

khadush
  • 1
  • 2

3 Answers3

0

As it is clearly mentioned in the error.

View function mapping is overwriting an existing endpoint function: search

That means, there is already a function named 'search'. I recommend you to change the function name like :

@app.route("/test")
def test():
    return "Hello"
0
from flask import Flask
app = Flask('name')

@app.route("/")
@app.route("/hello")
def hello_world():
    return "Hello, World!"

@app.route("/test")
def search(): return "Hello"

@app.route("/test/")
def search(search_query):  # Duplicate route for /test (hence the error function mapping is overwriting an existing endpoint)
    return search_query

Please delete the old search function that maps to /test. Also refer, AssertionError: View function mapping is overwriting an existing endpoint function: main

Sharad
  • 9,282
  • 3
  • 19
  • 36
0

Your code contains some bugs thats why it is giving you an error. It is not related to multiple search function as you don't have it. I made the changes below and it worked.

from flask import Flask

app = Flask(__name__)

@app.route("/") 

@app.route("/hello")

def hello_world(): 
  return "Hello, World!"

@app.route("/test") 
def search(): 
   return "Hello"

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


You can learn about these errors more here: What does if __name__ == "__main__": do?

Hope this helps !

Code run
  • 165
  • 9