我想在type value
OUTSIDE循环的.html
模板中发帖Model
。我可以在循环中获得价值。
我的模型看起来像:
的 models.py
class Post(models.Model):
TYPE = ( ("test", "test"), )
...
type = models.CharField(max_length=13, choices=TYPE, default="")
的 views.py
def post_type(request, type):
posts = Post.objects.filter(type=type)
return render(request, 'blog/post_type.html', {'posts': posts})
的 .html
{% block some_block %}
{{ posts.type}} # DOES NOT WORK - (Getting QuerySet[] only, but cannot call to {{posts.type}} or, let's say, {{ post.type[0] }} to just get that type.
{% for post in posts %}
{{ post.type }} # This works fine in Loop, cos Im inside set... (I can call even to post.title if defined in Model)
{% endfor %}
{% endblock %}
:(
-----编辑:----
{{posts.0.type}}
解决了问题
答案 0 :(得分:0)
这是因为:
posts = Post.objects.filter(type=type)
- 返回QuerySet
,而不是Post
的实例
你可以试试:
post1 = Post.objects.filter(type=type)[0]
- 获取type
Post
=MATCH("OPP",INDIRECT("A"&SUM($C$1:C1)+2&":A50"),0)
的价值。
答案 1 :(得分:0)
{{posts.0.type}}
也可以,谢谢
答案 2 :(得分:-1)
尝试在模板中使用它
{{ posts.0.type }}