How to do this complex search query in flask? -
I'm trying to implement an 'advanced search', but I'll keep it a bit easier for the question.
How it works so far:
@ app.route ('/ advanced_search', methods = ['GET', 'POST']) def advanced_search ( ): Form = Advanced Product Search () # This is a selectMultipleField countries [= 'Canada', 'France', 'Mexico', 'Nigeria'] form.categories.choices = [(C, C) countries) If the form .validate_on_submit (): countrydata = form.countsries.data res = models.product.query.filter (models.Product.country.in_ (countrydata)). All () # Now I have my search results, what will I do with them?which should be used to provide results, but I Do not know how this should work.advanced_search_results, there was another scene for me:@ app.route ('/ advanced_search_results', methods = ['GET', 'POST']) Def advanced_search_results (query): # What to do?1) In any way, pass all the rows in the data to see the
advanced_search_results (), but I do not think how I can do it I am2) Search the desired search query by
advanced_search_results ()and there DB searches. I am also not sure how to know about it.
Just return the required view:
All () # Now I have my search results, Let's go back to them, render_template ('advanced_search_results.')Then, in your view, you can loop in comparison to the results, as you do for any other list of models:
Ex> {% result result%} {{result.name}}
{{Result.country}}
& lt; Hourly Nouveau> {% Endfor%} Alternatively, if you have it at another URL To point to Advanced-search-results on the page, simply change verb to switch to advanced search .
& quot; form action = "/ advanced_search_results" method = "post" & gt; {# etc ... #} & lt; / form & gt; You can then validate the search and either display the result or redirect the user with an error message:
@ app.route ('/ advanced_search_results', [Widow = ['Post']) def Advanced_search_application (): form = advanced answer Footprints () countries = ['Canada', 'France', 'Mexico', 'Nigeria']] form.categories.choices = [c (c, c) in countries] if not form.validate_on_submit (): Flash ("There were errors in your submission") Redirect Reader (url_for ('advanced_search')) countrydata = form.countries.data res = models.Product.query.filter (Models.Product.country.in_ (countrydata)). All () returning render_template ('advanced_search_results.html', title = 'search result', result = res) This is your advanced_search method simplified:
Comments
Post a Comment