I have a News model, where each instance has a unique date. Which means that every day has exactly one News-instance related to it. I am redirecting to the detail-view of the News-instance from the navbar.
To achieve this am I using a context_processor. The context_processor.py file looks like this:
from news.models import TodayNews
import datetime
def todays_news(request):
todays_news = TodayNews.objects.get(date=datetime.date.today())
return {'news': todays_news}
This works completely fine as long as the News model has an instance on that particular date. Otherwise it returns the following error (which is very logical):
TodayNews matching query does not exist.
My question is, how can I do something different if the query is empty, for example if one particular date does not have a news-instance? What should I return instead of 'news': todays_news?