我收到了一个错误:
/ accounts / tcresults的TypeError 'ImageAndUser'对象不可迭代。
我想在tc.html中显示来自数据库的用户数据,但是会发生此错误。 我在views.py中写道
def tc(request):
d = {
'tcresults': ImageAndUser.objects.filter(user=request.user).order_by('id').last(),
}
return render(request, 'registration/accounts/tc.html', d)
在tc.html中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tc</title>
</head>
<body>
<h2>
{% for result in tcresults %}
{{ result.tc }}
{% endfor %}
</h2>
</body>
</html>
我想在这部分显示数据
{% for result in tcresults %}
{{ result.tc }}
{% endfor %}
我可以理解这个错误含义,因为我指定了最新的用户数据,所以数据只有一个而且不能重复。但我不知道如何解决这个问题。怎么写呢?
Traceback说
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Desktop/accounts/views.py" in tc
142. return render(request, 'registration/accounts/tc.html', d)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
68. return template.render(context, request)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/backends/django.py" in render
66. return self.template.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
208. return self._render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in _render
199. return self.nodelist.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render
994. bit = node.render_annotated(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/base.py" in render_annotated
961. return self.render(context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/template/defaulttags.py" in render
165. values = list(values)
答案 0 :(得分:1)
该对象不可迭代,因为:
ImageAndUser.objects.filter(user=request.user).order_by('id').last()
此过滤条件仅返回列表中的最后一个对象。一个结果元素不可迭代。
我建议您修改代码并尝试打印查询集中的项目数。
def tc(request):
tcresults = ImageAndUser.objects.filter(user=request.user).order_by('id').last()
print tcresults, ' queryset'
print len(tcresults), ' length'
d = {
'tcresults': tcresults,
}
return render(request, 'registration/accounts/tc.html', d)
并像这样更新您的HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tc</title>
</head>
<body>
<h2>
{{ tcresults.tc }}
</h2>
</body>
</html>
答案 1 :(得分:0)
从上面的代码中删除查询中的.last()
。