2

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 :

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'

In 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 *

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)

  • "my purpose is to be able to separate my json in keys/values to only display what it seems to me necessary" : actually there's nothing like a "json" object involved - at runtime, `post.publis` is a plain Python dict, so the usually lookup rules apply in your templates (ie : `post.publis.UT` will be resolved as `post.publis["UT"]`). – bruno desthuilliers Apr 19 '17 at 11:04

1 Answers1

6

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 %}

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 %}
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • "To access the keys in the JSON field " : actually at this point it's not a `JSONField` but a plain python dict... (ok, pedantic I guess but at least technically correct - which as we all know is "the best kind of correct" ) – bruno desthuilliers Apr 19 '17 at 10:54
  • 1
    @brunodesthuilliers since the issue was unrelated to the field type, I've removed the mention of JSONField from the answer. – Alasdair Apr 19 '17 at 11:00
  • thank you very much ! I had tried something similar. However, it had not worked before. – dorian verriere Apr 19 '17 at 11:00
  • This is good, but what if you don't know the names of the keys and just want to loop `key -> dict[key]` ? – Shadur Feb 07 '23 at 08:52
  • @Shadur you can loop through the items, e.g. `{% for key, value in mydict.items %}{{ key }}: {{ value }}{% endfor %}` – Alasdair Feb 07 '23 at 17:37