0

I've added the following function in context_processors.py. but when i go to index url, return me error. it's a natural error. because we do not have a date arguments in index.

is there any way to prevent this error to be displayed?

sample_path = http://127.0.0.1:8000/reserve/2017-02-01/

def one_day_foods_NOT_PAID(request):
    user = request.user
    path = request.get_full_path()
    date = path.strip('/').split("/", 2)
    if len(date) == 0:
        year, month, day = 0
    else:
        year, month, day = date[1].split("-")
        foods = Reservation.objects.filter(order_date__startswith = 
            datetime.date(int(year), int(month), int(day))).filter(user=user)
    return {'foods':foods}
shahin
  • 159
  • 1
  • 7

1 Answers1

0

Solve it:

def one_day_foods_NOT_PAID(request):
    user = request.user
    path = request.get_full_path()
    date = path.strip('/').split("/")
    try:
        year = date[1].split("-")[0]
        month= date[1].split("-")[1]
        day = date[1].split("-")[2]
        foods = Reservation.objects.filter(order_date__startswith = 
            datetime.date(int(year), int(month), int(day))).filter(user=user)
        return {'foods':foods}
    except IndexError:
        foods = None
        return {'foods':foods}
shahin
  • 159
  • 1
  • 7