简体   繁体   English

JSONField Django模板未显示我的操作

[英]JSONField Django template doesn't show me what I went

after a lot of research, I still have not found how to do it: my purpose is to be able to separate my json in keys/values to only display what it seems to me necessary (for example the title, the authors, ...). 经过大量研究,我仍然没有找到如何做的:我的目的是能够将我的json分隔为键/值,以仅显示我认为必要的内容(例如标题,作者,.. )。 It's a Django Site web. 这是Django Site网站。 That I have done : 我已经做到了:

In models.py 在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'

In Views.py 在Views.py中

def post_json(request):
    posts = Production.objects.all()
    return render(request, 'appli/post_json.html', {'posts': posts})

*And the template : post_json.html * *和模板:post_json.html *

This show me fully my json data 这充分显示了我的json数据

    {% for post in posts %}
    <div>
        <p>aa = {{ post.publis }}</p>
    </div>
    {% endfor %}

And this it's what I'm trying to show only the authors 这就是我要向作者展示的内容

 <h1>Publications lalala</h1>
         {% for post in posts %}
                aa = {{ post.publis }}
            <p> Num : {{ aa.UT }}</p>
            <p>Auteur : {{ aa.AU }} </p>
         {% endfor %}

The display on my web page : enter image description here 我的网页上的显示: 在此处输入图片描述

Thank you in advance for your help (sorry if there are mistakes of english I am french) 预先感谢您的帮助(对不起,如果有英文错误,我是法语)

To access the keys from post.publis in the Django template you use the regular dot lookup, eg {{ post.publis.UT }} . 要从Django模板中的post.publis访问密钥,请使用常规的点查找,例如{{ post.publis.UT }}

{% for post in posts %}
    <p>Num : {{ post.publis.UT }}</p>
    <p>Auteur : {{ post.publis.AU }} </p>
{% endfor %}

Putting aa = {{ post.publis }} in your template does not assign post.publis to aa . 在模板中放置aa = {{ post.publis }}不会将post.publis分配给aa If you want to prevent the duplication of post.publis , you can use the with tag. 如果要防止post.publis重复, post.publis可以使用with标记。

{% for post in posts %}
  {% with aa=post.publis %}
    <p>Num : {{ aa.UT }}</p>
    <p>Auteur : {{ aa.AU }} </p>
  {% endwith %}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM