Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django-从模型中获取值_Django_Django Models_Django Templates - Fatal编程技术网

Django-从模型中获取值

Django-从模型中获取值,django,django-models,django-templates,Django,Django Models,Django Templates,我想从Model外部循环获取模板.html中的post类型值。我可以在循环中得到值 我的模型看起来像: models.py: class Post(models.Model): TYPE = ( ("test", "test"), ) ... type = models.CharField(max_length=13, choices=TYPE, default="") def post_type(request, type): posts = Post.obj

我想从
Model
外部循环获取模板
.html
中的post
类型值。我可以在循环中得到值
我的模型看起来像:
models.py

class Post(models.Model):
    TYPE = ( ("test", "test"), )
    ...
    type = models.CharField(max_length=13, choices=TYPE, default="")
def post_type(request, type):
    posts = Post.objects.filter(type=type)
    return render(request, 'blog/post_type.html', {'posts': posts})
{% 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 %}

视图.py

class Post(models.Model):
    TYPE = ( ("test", "test"), )
    ...
    type = models.CharField(max_length=13, choices=TYPE, default="")
def post_type(request, type):
    posts = Post.objects.filter(type=type)
    return render(request, 'blog/post_type.html', {'posts': posts})
{% 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 %}

.html

class Post(models.Model):
    TYPE = ( ("test", "test"), )
    ...
    type = models.CharField(max_length=13, choices=TYPE, default="")
def post_type(request, type):
    posts = Post.objects.filter(type=type)
    return render(request, 'blog/post_type.html', {'posts': posts})
{% 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}
解决问题

发生这种情况的原因是:
posts=Post.objects.filter(type=type)
-返回
QuerySet
,而不是
Post
的实例
您可以试试:

post1=Post.objects.filter(type=type)[0]
-若要获取第一次
Post的
type
值,请尝试在模板中使用该值

{{ posts.0.type }}

{{posts.0.type}}
也可以,谢谢