【发布时间】:2018-10-15 05:09:19
【问题描述】:
我想从Model OUTSIDE 循环在我的模板.html 中发布type value。我可以在循环中获得价值。
我的模型看起来像: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}} 解决问题
【问题讨论】:
标签: django django-models django-templates