Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3623704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:24:59+00:00 2026-05-18T23:24:59+00:00

I have a question in Django on how you can compare dates to solve

  • 0

I have a question in Django on how you can compare dates to solve some solutions. For example I have a datefield in my models.py Like below.

class Invoice(models.Model):
    payment_date = models.DateTimeField()

What I want to be able to do is ask if the is a way to compare a datetime.now with a DateTimeField. For example, if I had a list of payment dates and I wanted to compare with datetime now. Thhe payment_date’s that are late with their payments are shown in owing. Otherwise, it the value is zero.

Here is my views to show whats going on. I have tried so far but I get a 0 value for payment_date’s which are later than the payment date.

Edit here is my latest views. Funny thing is that I seem to be getting the owing = invoice_gross for all results – unlike before when I was getting all 0s. So it is still not working properly.

@login_required
def homepage(request):
    invoices_list = Invoice.objects.all()
    invoice_name = invoices_list[0].client_contract_number.client_number.name
    invoice_gross = invoices_list[0].invoice_gross
    payment_date = invoices_list[0].payment_date
    if payment_date <= datetime.now():
        owing = invoice_gross
        if payment_date > datetime.now():
            owing = 0
    return render_to_response(('index.html', locals()), {'invoices_list': invoices_list ,'invoice_name':invoice_name, 'invoice_gross':invoice_gross,'payment_date':payment_date,'owing':owing}, context_instance=RequestContext(request))

Oh and my table is basically doing something like this.

ID  Owing
1   100   (All the same value)
2   100
3   100
.   .
.   .
.   .
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:25:00+00:00Added an answer on May 18, 2026 at 11:25 pm

    I think the problem is in the line

    if datetime.now() == payment_date:
    

    That will literally see if the payment_date is right now. I think you want to see if now is greater than or equal to the payment_date, in which case you should use

    if datetime.now() >= payment_date:
    

    You can also just filter the invoices when you query the database:

    invoices_list = Invoice.objects.filter(payment_date__lte=datetime.now())
    

    Update

    Your code is wrong because you have mutually exclusive conditionals. Look:

    if payment_date <= datetime.now():
        owing = invoice_gross
        if payment_date > datetime.now():
            owing = 0
    

    That first checks to see if payment_date is before now. Then it sets owing to invoice_gross. Then, in the same conditional, it checks to see if payment_date is after now. But that can’t be! You are only in this block of code if payment_date is before now!

    I think you have an indentation error, and want this instead:

    if payment_date <= datetime.now():
        owing = invoice_gross
    if payment_date > datetime.now():
        owing = 0
    

    Which, of course, is the same as:

    if payment_date <= datetime.now():
        owing = invoice_gross
    else:
        owing = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for some crazy subj. I'd like to override django models save method and
I have the following django model (mapped to table 'A'): class A(models.Model): name =
I want port some my Python scripts into web apps so that others can
Sorry for totally stupid question, but the situation is that I have to make
From the moment I have faced Python, the only thing I can say for
Im using python markdown for my django project, when i have the value #/usr/bin/env
Please forgive me if this question is ridiculous. I'm relatively new to programming and
All the answers I've seen to this so far have confused me. I've made
I realize this is more of a server question (since all media requests bypass
I'm trying to setup a project that contains commons apps (projectA) to be shared

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.