Flask blueprint and registering path

avani jain

I am trying to create route and method for it with parameters. Some how I am getting error as "The requested URL was not found on the server."

If I dont pass parameters everything works fine.

app.py has below code :

def create_app():

    app = Flask(__name__)
    app.secret_key = 'ytestsone'
    app.register_blueprint(auth, url_prefix='/')
    app.register_blueprint(search, url_prefix='/search')
    return app

Search.py I have below:

search = Blueprint("search", __name__, static_folder='static', template_folder='templates')

#@search.route("/") -->this works
@search.route("/<search_key>") --> this does not work
def search_page(search_key):    --> this does not work
#def search_page(): --> this works 
    print(request.form)
    return render_template("search.html", user=current_user)

in my html I have below

<div class="col">
            <a class="btn btn-primary btn-lg" href="/search" role="button">Search Item</a>
        </div>

can someone please help me what is wrong here with passing parameters?

NoCommandLine
  1. Since you have url_prefix in your blueprint, it means the system is looking for all routes that start with that value i.e. /search. See this writeup

  2. If your flask route handler is /<search_key>, it means Flask is actually expecting a url of type /search/<search_key> but the url for your button only has /search. Therefore there is no handler for it in your App.

  3. You should try this

@search.route("/") 
@search.route("/<search_key>") 
def search_page(search_key=None):

The above will work for /search i.e. @search.route("/") and also work for /search/abc i.e. @search.route("/<search_key>"). But you'll have to add code to take care of when search_key is None

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

'function' object has no attribute 'name' when registering blueprint

From Dev

Calling method from Flask Blueprint

From Java

flask blueprint template folder

From Dev

How to pass arbitrary arguments to a flask blueprint?

From Dev

Flask: Template in Blueprint Inherit from Template in App?

From Dev

Flask - A blueprint's name collision occurred

From Dev

Registering route on blueprint raises AttributeError: 'function' object has no attribute 'route'

From Dev

How to init restless as flask blueprint

From Dev

Flask Session not persistant in blueprint

From Dev

Flask import not registering route

From Dev

Overwrite route in flask blueprint

From Dev

Flask Blueprint 404

From Dev

Flask BluePrint route not working with multiple parameteres

From Dev

Why is Aries Blueprint not registering a namespace handler for camel-cxf and camel-blueprint?

From Dev

Flask: Blueprint not showing custom error page

From Dev

how do I redirect to the flask blueprint parent?

From Dev

Flask python blueprint logic code separation

From Dev

Registering server events with flask SocketIO

From Dev

Can't get flask blueprint to work

From Dev

Flask Blueprint structure

From Dev

Flask Blueprint pass object to another file

From Dev

Flask: get current blueprint webroot

From Dev

Flask - Forms - Blueprint

From Dev

Query while declaring Flask Blueprint

From Dev

Running a Flask blueprint from the program

From Dev

flask, registering the same blueprint multiple times with different database connections, same model

From Dev

Blueprint error in Flask: NameError: name of blueprint is not defined

From Dev

Flask Blueprint Subdomain Not Working GAE

From Dev

Inject dependencies in Flask blueprint

Related Related

HotTag

Archive