Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading