I'm building a website in python with flask.
How can I pass text from a form in an html file to a url route in my python file?
I have a search button that passes text to a python 'page' which queries a database and then render_templates an html file with the relevant object from my database (which works)...
<form class="d-flex" action="/search" method="POST">
<input id="search" name="search" class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-light" type="submit"><i class="fa-solid fa-magnifying-glass"></i></button>
</form>
@pages.route('/search', methods=['POST'])
def search_page():
search = request.form.get('search')
search_url = search.replace(' ', '-').lower()
query database with 'search_url' and get search_object...
return render_template('search_object.html', search_object=search_object)
My issue is that I want my url to be whatever the search_url text is, and not static '/search'
So when I search 'example search text' I would go to a page with a url of 'example.com/example-search-text'
I know you can pass a variable with curly brackets / to the page.route /< > ...
So I guess I'm asking, how do I take the search text and link it to the search action in my html file?
source https://stackoverflow.com/questions/75709482/how-can-i-pass-text-from-a-form-in-an-html-file-to-a-url-route-in-my-python-file
Comments
Post a Comment