Python django模板中的时间限制
我需要时间限制方面的帮助。我想向用户展示他需要多少时间才能租到一辆车。 以下是我的看法:Python django模板中的时间限制,python,django,django-templates,django-views,Python,Django,Django Templates,Django Views,我需要时间限制方面的帮助。我想向用户展示他需要多少时间才能租到一辆车。 以下是我的看法: class CarRentView(RedirectView): permanent = False query_string = True model = Car def date_of_return(request): car = Car.objects.all() car.rent = True date_of_rent = datetime.now
class CarRentView(RedirectView):
permanent = False
query_string = True
model = Car
def date_of_return(request):
car = Car.objects.all()
car.rent = True
date_of_rent = datetime.now()
date_of_rent = car.save()
car_return_date = date_of_rent+timedelta(days=30)
return render(request, 'cars','car_return_date')
当我想在模板中执行此操作时:
{{ car_return_date }}
什么都没有,我不知道怎么了。是否可以显示返回日期并在此之后进行计数?您需要以不同方式调用渲染函数:
return render(request, 'myapp/index.html', {'cars': car, 'car_return_date': car_return_date})
有关更多信息,请参阅
此外,我认为您没有正确设置
car.rent
,因为您正在所有cars上设置它。您需要以不同方式调用渲染功能:
return render(request, 'myapp/index.html', {'cars': car, 'car_return_date': car_return_date})
有关更多信息,请参阅
此外,我认为您没有正确设置
car.rent
,因为您正在所有cars上设置它。返回模板的变量需要作为上下文词典的一部分返回。所以你会:
return render(request, 'my_template.html', {'car': car, 'car_return_date': car_return_date})
car\u return\u date
变量将在模板中使用:
{{ car_return_date }}
在输出datetime
对象时,您可能还需要使用日期过滤器。例如,您可以使用:
{{ car_return_date|date:"j N Y" }}
返回模板的变量需要作为上下文字典的一部分返回。所以你会:
return render(request, 'my_template.html', {'car': car, 'car_return_date': car_return_date})
car\u return\u date
变量将在模板中使用:
{{ car_return_date }}
在输出datetime
对象时,您可能还需要使用日期过滤器。例如,您可以使用:
{{ car_return_date|date:"j N Y" }}
我建议将
租金日期
改为最后租金日期
,增加30天。然后,将租金的最后日期保存到模型中。因此,您可以直接在模板中使用,last\u date\u of\u rent
。此外,使用内置模板过滤器timeuntil
将返回所需的值。-
{{car.last_date_of u rent|timeuntil}}
文件:
我建议将
租金日期
更改为最后租金日期
,增加30天。然后,将租金的最后日期保存到模型中。因此,您可以直接在模板中使用,last\u date\u of\u rent
。此外,使用内置模板过滤器timeuntil
将返回所需的值。-
{{car.last_date_of u rent|timeuntil}}
文件:
这不是错误,{car\u return\u date}日期:teplate上的JNy}不显示任何日期
{car\u return\u date}
本身返回任何内容?{{car\u return\u date}没有返回任何内容这不是错误,{car car\u return\u date日期:teplate上的JNy}不显示任何日期{car return date date}本身返回任何内容{{car\u return\u date}未返回任何内容