Skip to content Skip to sidebar Skip to footer

Is There An "if Command" To Identify If My Sqlalchemy Sqlite Query Command Is Null?

I have these codes below: many_posts = BlogPost.query.filter(or_((BlogPost.problem_name.ilike('%' + form.search.data + '%')),(BlogPost.text.ilike('%' + form.search.data + '%')))).o

Solution 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)

Solution 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.

Solution 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<spanclass="iconify"data-icon="icons8:search"data-inline="false"></span>:</h4>
{% if not posts %}
   <p> Couldn't find relating problems with your search </p>
{% else %}
  <divclass="container row row-cols-1 row-cols-md-2 text-center">
  {% for post in posts.items %}
    <divclass="card border-dark mb-3 "style="width: 20rem;"><divclass="card-body "><h7><aclass="text-warning"href="{{ url_for('users.user_posts', username=post.creator.first_name+post.creator.middle_name+post.creator.last_name) }}"><imgclass="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><imgclass="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><aclass="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><pclass="card-text">{{ post.text[0:100] }}</p><p><smallclass="text-muted">Posted on: {{ post.date.strftime('%Y-%m-%d') }}</small></p><aclass="btn btn-warning"href="{{ url_for('blog_posts.blog_view', blog_validated_id=post.blog_id) }}">Read more</a></div></div>
  {% endfor %}
  </div>
{% endif %}

Post a Comment for "Is There An "if Command" To Identify If My Sqlalchemy Sqlite Query Command Is Null?"