dor*_*ere 2 python django postgresql json field
经过大量研究,我仍然没有找到如何做到这一点:我的目的是能够在键/值中分离我的 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'
Run Code Online (Sandbox Code Playgroud)
在 Views.py 中
def post_json(request):
posts = Production.objects.all()
return render(request, 'appli/post_json.html', {'posts': posts})
Run Code Online (Sandbox Code Playgroud)
*和模板:post_json.html *
这完全显示了我的 json 数据
{% for post in posts %}
<div>
<p>aa = {{ post.publis }}</p>
</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这就是我只想向作者展示的内容
<h1>Publications lalala</h1>
{% for post in posts %}
aa = {{ post.publis }}
<p> Num : {{ aa.UT }}</p>
<p>Auteur : {{ aa.AU }} </p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我的网页上的显示: 在此处输入图像描述
预先感谢您的帮助(如果有英语错误,我很抱歉,我是法国人)
post.publis要从Django 模板中访问键,您可以使用常规的点查找,例如{{ post.publis.UT }}.
{% for post in posts %}
<p>Num : {{ post.publis.UT }}</p>
<p>Auteur : {{ post.publis.AU }} </p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
放入aa = {{ post.publis }}您的模板不会分配post.publis给aa. 如果你想防止重复post.publis,你可以使用with标签。
{% for post in posts %}
{% with aa=post.publis %}
<p>Num : {{ aa.UT }}</p>
<p>Auteur : {{ aa.AU }} </p>
{% endwith %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2411 次 |
| 最近记录: |