I have these codes below:
many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc()).paginate(page=page, per_page=10)
return render_template('blog_search_result.html', id_list=id_list, many_posts=many_posts, no_posts=no_posts)
It's an sqlalchemy query command and it let users on my website to search/filter and query blogs. But sometimes, the input they give is not in the database and my website will not show anything. So, instead of not showing anything, I want to show a text says: "Couldn't find relating things to your search". I wonder if there is any "if statement" Python/Sqlalchemy codes I can use to identify if my query command didn't find any information and then to show the quote above. Something like:
if many_posts is null:
no_posts= "Couldn't find relating problems with your search"
Part of my HTML codes that connect with many_posts:
<h4>Search results<span class="iconify" data-icon="icons8:search" data-inline="false"></span>:</h4>
<p>{{ no_posts }}</p>
<div class="container row row-cols-1 row-cols-md-2 text-center">
{% for post in many_posts.items%}
<div class="card border-dark mb-3 " style="width: 20rem;">
<div class="card-body ">
<h7><a class="text-warning" href="{{ url_for('users.user_posts', username=post.creator.first_name+post.creator.middle_name+post.creator.last_name) }}"><img class="text-center rounded" src="{{ url_for('static', filename='profile_pics/'+ post.creator.profile_image) }}" width = "35" height = "35" alt=""> {{ post.creator.first_name}} {{ post.creator.middle_name }} {{ post.creator.last_name }}</a></h7>
<p></p>
<img class="text-center rounded responsive1" alt="" src="{{ url_for('static', filename='blog_pics/'+ post.blog_image) }}" width = "495" height = "250">
{# Need caution for post.blog_image on the code above #}
<p></p>
<h2><a class="card-tittle text-body problem" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">{{ post.problem_name[0:40]}}..</a></h2>
<p class="card-text">{{ post.text[0:100] }}</p>
<p><small class="text-muted">Posted on: {{ post.date.strftime('%Y-%m-%d') }}</small></p>
<a class="btn btn-warning" href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">Read more</a>
</div>
I'm a beginner in coding and I would greatly appreciate if you could help me. Thank you!
========================Answers/Solution=========================
I use the codes below:
Method 1:
many_posts0 = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())
many_posts = many_posts0.paginate(page=page, per_page=10)
num_posts = many_posts0.count()
if num_posts == 0:
no_posts="Couldn't find relating problems"
else:
no_posts=''
return render_template('blog_search_result.html', id_list=id_list, many_posts=many_posts, num_posts=num_posts, no_posts=no_posts)
In HTML, I use:
<p>{{no_posts}}</p>
Method 2:
many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())
no_posts = ''
if not many_posts.first():
no_posts= "Couldn't find relating problems with your search"
many_posts = many_posts.paginate(page=page, per_page=10)