django - 'QuerySet' 对象在使用 timesince 时没有属性 'year'

标签 django object django-views attributes django-queryset

'QuerySet' object has no attribute 'year'
Request Method: GET
Request URL:    http://127.0.0.1:8000/dashboard/
Django Version: 2.2.8
Exception Type: AttributeError
Exception Value:    
'QuerySet' object has no attribute 'year'

模型.py
joined_date = models.DateTimeField(default=now, editable=False)

View .py
date_joined = User.objects.order_by('-date_joined')

html
{{ date_joined|timesince }}

最佳答案

date_joined是一个查询集,一个都没有 User对象,所以你不能使用 timesince模板标签就可以了。

您可能应该遍历用户,或者发送一个 User反对模板并使用 timesince日期时间字段 ,而不是对象本身:

{% for user in date_joined %}
    {{ user.joined_date|timesince }}
{% endfor %}

关于django - 'QuerySet' 对象在使用 timesince 时没有属性 'year',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59290678/

相关文章:

django - 我无法更改 django drf 中的日期格式

javascript - 从数组和包含另一个数组的属性创建一个新数组

javascript - 获取对象中不包含在数组中的一个元素

python - Django 不解析自定义的 http 接受 header

python - 加入 Django View

python - Django + celery : how to chain tasks with parameters to periodic task

python - Django默认用户权限

python - 更改用户时如何不显示密码?

javascript - 如何轻松创建一个 javascript 2D 字典,其中键顺序无关紧要?

python - 无法在模板中显示外键中的相关字段