Title: Django Empty Queryset
Tags: django;django-queryset
Question: I want to send news variable to my template:
news = News.objects.all()
except:
#Is it programmatically true?
news = ""
Is it programmatically true that I send news as empty srting when the query is empty?
Comment: {% if news %} for loop {% endif %}  @RahulGupta

Comment: How do you use the `news` variable in your template?

Comment: What sort of exceptions do you intend to catch with `try-except`?

Here is the accepted answer: You can just send the news variable having value as News.objects.all() to your template.
There is no need to put a try-except block for News.objects.all() as it returns a QuerySet instance without raising an exception even if there were no objects in it (if that was the exception you were intending to catch).


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:10/25/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<django><django-queryset>', 'Title': 'Django Empty Queryset', 'ViewCount': 526, 'id': 33333708, 'user_AboutMe': '', 'user_AccountId': 149271.0, 'user_DisplayName': 'TheNone', 'user_Reputation': 5476.0, 'user_Views': 408.0, 'user_id': 361635.0}}