I can’t handle it for a long time.
it returns previous search (mySearch) when I try second one (mySecondSearch) in address – http://127.0.0.1:5000/search?key=mySearch
(but query works – I get template with list with key mySecondSearch)
how to get address with key that relevant my request
header.html
<form method="POST" action="{{ url_for('views.search', key=slugy) }}" role="search" class="hiddenSearch">
{{ search_form.hidden_tag() }}
<input class="form-control" type="search"
placeholder=""
aria-label="Search"
name="searched"
minlength="3"
maxlength="44"
oninvalid="this.setCustomValidity('')"
oninput="this.setCustomValidity('')"
title=""/>
</form>
the view.py
@views.route('/search/', methods=["POST"])
def search():
page_title = ""
clean = re.compile('<.*?>')
search_form = SearchForm()
posts = Poems.query
if request.method == "POST" and search_form.validate_on_submit():
# Get Data from submitted form
searched = search_form.searched.data
poem_list.searched = searched
slugy = slugify(searched)
# Query
page = request.args.get('page', 1, type=int)
posts = posts.filter(Poems.con_keyword.ilike('%' + searched + '%'))
posts = posts.order_by(Poems.title).paginate(per_page=7, page=page, error_out=True)
return render_template("content/search.html",
search_form=search_form,
searched=searched,
page_title=page_title,
posts=posts,
clean=clean, re=re, slugy=slugy)
else:
flash("!")
return render_template("content/search.html",
search_form=search_form,
page_title=page_title,
posts=posts, clean=clean, re=re)
search.html
<section id="search">
<div class="content">
{% if searched %}
<h1> you ask -
<span class="src-ed">{{ searched[0:12] }}...</span><br/>we found:
</h1>
...etc
I tried remove cache, or save no data, – nothing
tested – everything return something flawlessly
AI – nothing that can help me (I tried its code – works as mine)
It is crazy but the code were taken from free courses so basically written wrongly. I’ve read a lot(documentation) so solution is !!! re-write it!
so now it works !
http://127.0.0.1:5000/search?key=myNewSearch
code
@views.route('/search', methods=['GET', 'POST'])
def search():
page_title = "Поиск"
clean = re.compile('<.*?>')
search_form = SearchForm()
query = request.args.get('key')
if query:
posts = Poems.query.filter(Poems.con_keyword.ilike(f'%{query}%')).all()
else:
posts = Poems.query.all()
return render_template("content/search.html", search_form=search_form,
page_title=page_title, posts=posts,
clean=clean, re=re, key=query)
and of course html form should be GET
<form method="GET" action="{{url_for('views.search', key=searched)}}"
role="search"
class="hiddenSearch">
<input class="form-control"
type="search"
placeholder=""
aria-label="Search" name="key">
</form>