在 Django 中,我想从视图中更改日期

In django i want to change the date from views

我想从 Django 中的后端视图更新日期我该怎么做这是我想在其中执行此操作的视图 解释我真正想要实现的目标 我正在构建一个客户关系管理系统,客户应该根据他们的计划按月或按季度付款 我在 dashbord 上给了一个按钮,它触发了这个功能,如果用户日期月份来了,那么这个功能会重定向到主页功能是假设生成在主页中获取的账单并显示未付金额这个想法发生了什么错误每次按下按钮它会生成重复的账单增加收入以停止这个我添加一个变量是合格的我认为用户会手动更改它,但后来我觉得更新日期更好

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('/')

您可以使用 timedelta

在给定天数的基础上递增 datetime
    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.  

这是更新 datetime 的方法。其余的你可以根据需要实施。