0

I have an object in python views.py that references the events database table. When there is data, it displays the data in my html template, however, when there is no data, I cannot figure out the {% if %} function that would display the message "No data found."

I have tried Tadeck's post, but the is defined always seems to evaluate to true even if there is no data to display. Thank you for your help.

{% if events is defined %}
    value of variable: {{ events }}
{% else %}
    variable is not defined
{% endif %}

views.py

events = db.session.query(Eventdetails, Buyers).\
            join(Buyers).\
            filter(Eventdetails.events_id == event_id)

return render_template(
            self.template_file, events=events, the_event=the_event,
            event_id=event_id
        )
user3324136
  • 415
  • 5
  • 20

2 Answers2

1

You are passing events as the query. You want it to be the query results:

events = db.session.query(Eventdetails, Buyers).\
        join(Buyers).\
        filter(Eventdetails.events_id == event_id).all()

Things to try:

{% if events %}
{% if events|length > 0 %} 
{% if events != [] %}
GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • This has been helpful, but unfortunately, neither really worked. {% if events %} always evaluated to True, never showing the else message {% if events|length > 0 %} throws an error BaseQuery' has no len() {% if events != [] %} always evaluated to true, never showing the else message – user3324136 Sep 09 '20 at 20:37
1

This will help you. When event have data then it will go inside the if condition otherwise else will be execute No data found.

{% if events %}
    value of variable: {{ events }}
{% else %}
    No data found.
{% endif %}
Akshay Jain
  • 790
  • 1
  • 7
  • 21