TypeError:“datetime.date”对象不可下标
Posted
技术标签:
【中文标题】TypeError:“datetime.date”对象不可下标【英文标题】:TypeError: 'datetime.date' object is not subscriptable 【发布时间】:2019-12-07 13:54:12 【问题描述】:我正在尝试创建一个带有 3 个参数的自定义模板标签。我正在尝试计算两个日期之间的天数,同时不包括周末的天数。并且根据部门的不同,每个用户的周末都不同。所以我需要将start_date
、end_date
、user_id
传递给模板标签函数。这是我到目前为止所做的:
from django import template
from datetime import datetime, timedelta, date
register = template.Library()
@register.filter('date_diff_helper')
def date_diff_helper(startdate, enddate):
return [startdate, enddate]
@register.filter(name='date_diff')
def date_diff(dates, user_id):
start_date = dates[0]
end_date = dates[1]
count = 0
weekends = ["Friday", "Saturday"]
for days in range((end_date - start_date).days + 1):
if start_date.strftime("%A") not in weekends:
count += 1
else:
start_date += timedelta(days=1)
continue
if start_date == end_date:
break
start_date += timedelta(days=1)
return count
这就是我在模板中调用这些函数的方式:
leave.start_date|date_diff_helper:leave.end_date|date_diff:leave.emp_id
当我运行代码时,它给了我TypeError
说'datetime.date' object is not subscriptable
。当我尝试在date_diff
函数中检查dates
参数的类型时,它说:
但是当它尝试将 start_date 指定为第一个日期对象时,如 start_date = dates[0],它甚至会抛出错误。这是错误的完整回溯:
Traceback:
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
34. response = get_response(request)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
115. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
113. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in view
71. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\views\generic\base.py" in dispatch
97. return handler(request, *args, **kwargs)
File "C:\Projects\LMS\LMSAdmin\views.py" in get
203. return render(request, self.template_name, 'leave_requests': leave_requests)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader.py" in render_to_string
62. return template.render(context, request)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py" in render
61. return self.template.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
171. return self._render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py" in render
150. return compiled_parent._render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in _render
163. return self.nodelist.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
937. bit = node.render_annotated(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\defaulttags.py" in render
209. nodelist.append(node.render_annotated(context))
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render_annotated
904. return self.render(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in render
987. output = self.filter_expression.resolve(context)
File "C:\Users\Naeem.Khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py" in resolve
698. new_obj = func(obj, *arg_vals)
File "C:\Projects\LMS\LMSAdmin\templatetags\LMSAdmin_tags.py" in date_diff
38. start_date = dates[0]
Exception Type: TypeError at /lms_admin/SeniorManagementAdmin/
Exception Value: 'datetime.date' object is not subscriptable
我是 Django 和 Python 的初学者。
编辑
这就是我检查dates
变量类型的方式:
def date_diff(dates, user_id):
print(type(dates))
#if I removed these two lines, the result is only < class 'list'>
start_date = dates[0]
end_date = dates[1]
...
当我访问该页面时,它会在控制台中打印出类型是列表和日期时间。但是如果我删除上面的 start_date 和 end_date 变量,它只会打印出
【问题讨论】:
"当我试图检查 date_diff 函数中日期参数的类型时,它说: " => 你是如何“检查”的?为什么你有一个列表和一个日期时间? 很抱歉回复晚了。我已经编辑了帖子以显示我是如何获得这些类型的。 我仍然无法理解print(type(dates))
(您可以使用print(dates)
FWIW)如何产生您发布的结果 - 您应该有一个结果,它应该是< class 'list'>
。我怀疑此过滤器在您模板的其他地方第二次使用,并应用于错误的值。
你好。你说的对。由于前端页面太大,我什至没有考虑过。我真笨。太感谢了。确实是这个标签用错了地方的问题。
请将其发布在答案中,以便我接受。它可能会对某人有所帮助。
【参考方案1】:
当我运行代码时,它给了我 TypeError 说 'datetime.date' 对象不可下标。当我试图检查 date_diff 函数中的日期参数类型时,它说:
这表明您实际上有 两个 调用模板中的过滤器 - 第一个是正确的,第二个是不正确的。
【讨论】:
以上是关于TypeError:“datetime.date”对象不可下标的主要内容,如果未能解决你的问题,请参考以下文章
TypeError: 不支持的操作数类型 -: 'datetime.date' 和 'str'