Saturday, 25 January 2020

How to replace spaces in URL having path parameter, with a hyphen or an underscore

I am sorry if this Q makes no sense , but is there a way to replace spaces with hyphen in URL's(only) having used path parameter to build it ?

My scenario is as :

I have a view method as below:

from app.service import *

@app.route('/myapp/<search_url>',methods=['GET','POST'])
def search_func(search_url):
    print(search_url) // This prints 'hi-martin king' and i need to pass 'hi-martin king' to below with preserving space
    search_q = search(search_url) 
    return render_template('wordsearch.html',data=search_q['data'])
  • here search_url I am passing from template

I have the search function which takes search_url as argument(def search(search_url): .....) and does all operations (for ref) which i have imported from service above.

Now when I run , i have the sample URL as,

....myapp/hi-martin%20king

Here I am preserving space to perform query in database (In db it is stored as martin king), but I don't want to show the same in URL instead replace it with a hyphen

I have other way that changing all the values in database (removing spaces but this is tmk not a appropriate solution)

expected o/p:

....myapp/hi-martin-king  (or with an underscore) ....myapp/hi-martin_king 

Here how can i preserve spaces to pass it as argument to the function and at the same time I want to replace only in URL ? Is this possible ?

Any help is appreciated ....TIA



from How to replace spaces in URL having path parameter, with a hyphen or an underscore

No comments:

Post a Comment