错误 - 未支撑的操作数类型(S)用于 - :' str' and' dateTime.timedelta'在Django
大家好,我在Django-framework上工作,我是在函数上创建的,在该函数上,我可以在parameter start_date和end_date上,我尝试根据start_date获得上一周的start_date和end_date 错误 - 未支撑的操作数类型 - :'str'和'dateTime.timedelta'
,请帮助我。
from datetime import datetime, timedelta, date
def report(request):
start_date = request.GET.get('start_date')
print(start_date) # 2022-06-06
end_date = request.GET.get('end_date')
print(end_date) # 2022-06-12
last_week_start_date=start_date - timedelta(days=7)
last_week_end_date=last_week_start_date + timedelta(days=6)
d_report = connection.cursor()
d_report.execute('''select * from table where start_date=%s and end_date=%s''',[start_date,end_date])
Hi Everyone i have working on Django-framework, I am created on function where i give parameter start_date and end_date, i am trying get previous week start_date and end_date based on start_date but getting
Error - unsupported operand type(s) for -: 'str' and 'datetime.timedelta'
, please help me out.
from datetime import datetime, timedelta, date
def report(request):
start_date = request.GET.get('start_date')
print(start_date) # 2022-06-06
end_date = request.GET.get('end_date')
print(end_date) # 2022-06-12
last_week_start_date=start_date - timedelta(days=7)
last_week_end_date=last_week_start_date + timedelta(days=6)
d_report = connection.cursor()
d_report.execute('''select * from table where start_date=%s and end_date=%s''',[start_date,end_date])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
start_date和end_date是字符串。您应该将它们转换为DateTime对象。这应该是ISO格式,因此您可以使用dateTime.fromisoformat()
start_date and end_date are strings. You should convert them to a datetime object. This should be the ISO format so you can use datetime.fromisoformat()
问题是
start_time
是str
类型变量。您必须首先将其转换为datetime
对象,然后才能将其使用。要转换,您可以使用此代码库
The problem is
start_time
is astr
type variable. You have to convert it todatetime
object first, only then you can use timedelta with that.To convert, you can use this codebase