经过大量研究,我仍然没有找到如何做到这一点:我的目的是能够在键/值中分离我的 json,以仅显示我认为必要的内容(例如标题、作者、.. .)。这是一个 Django 网站。
我已经做到了:
在 models.py 中
class Production(models.Model):
titre = models.CharField(max_length=255, blank=True)
publis = JSONField()
def __str__(self):
return '%s' % (self.titre)
class Meta:
db_table = 'Production'
在 Views.py 中
def post_json(request):
posts = Production.objects.all()
return render(request, 'appli/post_json.html', {'posts': posts})
*和模板:post_json.html *
这完全显示了我的 json 数据
{% for post in posts %}
<div>
<p>aa = {{ post.publis }}</p>
</div>
{% endfor %}
这就是我只想向作者展示的内容
<h1>Publications lalala</h1>
{% for post in posts %}
aa = {{ post.publis }}
<p> Num : {{ aa.UT }}</p>
<p>Auteur : {{ aa.AU }} </p>
{% endfor %}
我的网页上的显示:在此输入图像描述
预先感谢您的帮助(如果有英语错误,我很抱歉,我是法国人)
要从以下位置访问密钥post.publis
在 Django 模板中,您使用常规的点查找,例如{{ post.publis.UT }}
.
{% for post in posts %}
<p>Num : {{ post.publis.UT }}</p>
<p>Auteur : {{ post.publis.AU }} </p>
{% endfor %}
Putting aa = {{ post.publis }}
在你的模板中没有分配post.publis
to aa
。如果你想防止重复post.publis
,您可以使用with tag.
{% for post in posts %}
{% with aa=post.publis %}
<p>Num : {{ aa.UT }}</p>
<p>Auteur : {{ aa.AU }} </p>
{% endwith %}
{% endfor %}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)