问题
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)
回答1:
Not sure, but you might try two things:
Perhaps the result of a query doesn't evaluate to False
if no row is returned. an empty list evaluates to False
, but query results are probably objects of a different type and might not evaluate to False.
converting to a list might help.
return render_template('blog_search_result.html', id_list=id_list, posts=list(posts))
for debugging purposes. you could have added following. Printing / logging is quite helpful to understand
print("Posts is ", posts, "and as bool it returns ", bool(posts))
return render_template('blog_search_result.html', id_list=id_list, posts=list(posts))
After first feedback
OK the output of SqlAlchemy paginate()
is not iterable. (Did you try to read the doc of paginate. It might mention whether there is some info about amount of results)
Following might work with SqlAlchemy (I don't have time to try)
posts_to_show = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())
post_page = posts_to_show.paginate(page=page, per_page=10)
num_posts = posts.count() # there might be something better to find out if there is at least one post, but I don't know it
return render_template('blog_search_result.html', id_list=id_list, posts=posts_page, num_posts=num_posts)
回答2:
There are many ways to do what you are trying to accomplish here! You can check if there is no data in your query like this:
many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc()) #without paginate
no_posts = None
#check if doesn't have anything inside
if not many_posts.scalar():
no_posts= "Couldn't find relating problems with your search"
many_posts = many_posts.paginate(page=page, per_page=10)
You can read more about scalar() here.
回答3:
You need not
operator in if condition:
if not <variable>:
#block_code
And you don't need to declare or pass no_posts
to render_template. You can achieve your desired result by doing something this just in your jinja code html template only.
foobar.py
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, posts=posts)
blog_search_result.html
<h4>Search results<span class="iconify" data-icon="icons8:search" data-inline="false"></span>:</h4>
{% if not posts %}
<p> Couldn't find relating problems with your search </p>
{% else %}
<div class="container row row-cols-1 row-cols-md-2 text-center">
{% for post in 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>
</div>
{% endfor %}
</div>
{% endif %}
来源:https://stackoverflow.com/questions/61904126/is-there-an-if-command-to-identify-if-my-sqlalchemy-sqlite-query-command-is-nu