Home > Mobile >  In django i want to change the date from views
In django i want to change the date from views

Time:09-28

I want to update the date from the backend views in django how can i did that here are my views where i want to do this explaining what i exactly want to achive i am building a crm where clients suppose to pay monthly or quaterly depends on their plan i have given a button at dashbord which triggers this function and redirect to homepage in this function if the user date month came then this function is suppose to generate the bills which is fetching in the homepage and show outstanding amount what wrong happen with this idea everytime pressing the button it will genertate duplicate bills adding in revenue to stops this i add a variable is eligible which i thought the user will change it manually but then i feel it is more good to update the date

def refresh_dashboard(request):
    date = datetime.datetime.now().strftime ("%Y%m%d")
    m = datetime.date.today()
    print(f"the date is {m}")
    customer = Customer.objects.all()
    for i in customer:
        # print(i.eligible)
        period = i.next_payment.strftime("%Y%m%d")
        
        if period <= date and i.eligible == True:
            x = Bill.objects.create(name = i.name,status ="unpaid",price = i.recuring_amount,generate_date = date)
            x.save()
            obj = i
            # obj.next_payment.strftime("%Y%(m 1)%d")
            obj.eligible = False
            obj.save()
            # print(f"the date is {date} and the obtain from the customer is {period}")
            # print(f"this customer  {i.name} bill need to be generated")
    # print(f"the date is {datetime.datetime.now()}")
    return redirect('/')

CodePudding user response:

You can increment datetime on the basis of given days using timedelta

    from datetime import datetime  
    from datetime import timedelta #new

    today = datetime.now() 
    print(f"Today's the date & Time is {today}")
    month_later = today  timedelta(days=MONTHLY_CYCLE)
    three_months_later = today  timedelta(days=QUA_CYCLE)
    six_months_later = today  timedelta(days=SIX_MONTH_CYCLE)
    print(f"three_months_later's the date & Time is {month_later}")
    print(f"three_months_later's the date & Time is {three_months_later}")
    print(f"six_months_later's the date & Time is {six_months_later}")
    
    customer = Customer.objects.get(pk=id) # Targeted Customer 
    selected_cycle = int(customer.billing_cycle) #return the value of billing_cycle selected from Customer
    tentative_date = today  timedelta(days=selected_cycle) 
    print(f"tentative_date Billing date & Time is {month_later}") # Required DAte.  

This is how you can update the datetime. rest you can implement as required.

  • Related