Home > Mobile >  What is the output format of datefield to us it in strptime?
What is the output format of datefield to us it in strptime?

Time:02-11

I need to display three upcoming college fests in homepage, so I need to compare start_date of each fest with today's date and display it if it's equal or greater than today's date

The code I need this for is(here start_date is of type DateField)

def home(request):
    context={
        'fests':Fest.objects.all().filter(datetime.strptime('start_date',"%b %d ")>=date.today()).order_by('-start_date')[:3]
    }
    return render(request,'webpage/home.html',context)

The output of start_date is in str form, so I can't compare it with date.today(). To use strptime() I need to input the format of the string. So what is the format needed?

CodePudding user response:

It has no format, it is simply a date object, so you filter with:

def home(request):
    context={
        'fests': Fest.objects.filter(start_date__gte=date.today()).order_by('-start_date')[:3]
    }
    return render(request,'webpage/home.html',context)
  • Related