JSONField Django模板未向我显示我去了什么 [英] JSONField Django template doesn't show me what I went

查看:137
本文介绍了JSONField Django模板未向我显示我去了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量研究,我仍然没有找到如何做的:我的目的是能够将我的json分隔为键/值,以仅显示我认为必要的内容(例如标题,作者,...).这是Django Site网站. 我已经完成了:

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, ...). It's a Django Site web. That I have done :

在models.py

In 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

In Views.py

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

*还有模板:post_json.html *

这完全向我展示了我的json数据

This show me fully my json data

    {% 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)

推荐答案

要使用Django模板中的post.publis访问键,请使用常规的点查找,例如{{ post.publis.UT }}.

To access the keys from post.publis in the Django template you use the regular dot lookup, e.g. {{ post.publis.UT }}.

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

在模板中放置aa = {{ post.publis }}不会将post.publis分配给aa.如果要防止重复post.publis,则可以使用 with 标签.

Putting aa = {{ post.publis }} in your template does not assign post.publis to aa. If you want to prevent the duplication of post.publis, you can use the with tag.

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

这篇关于JSONField Django模板未向我显示我去了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆