我试图找到答案,但找不到。当我迭代时在我的模板中JSONfield
{% extends "electronic/electronic_base.html" %}
{% load staticfiles %}
{% block content %}
{% for item in micro_all %}
<h1>{{item.title}}</h1>
<table class="table table-condensed">
<tbody>
<tr>
{% for key in item.attributes%}
<td>{{key}}</td>
{% endfor %}
</tr>
</tbody>
</table>
{% endfor %}
{% endblock %}
我可以得到钥匙,但不能得到价值。尝试使用键、值和打印值,但由于某些原因,它给了我单个字母,甚至不是表格格式。如何访问模板中的值
from django.db import models
from django.contrib.postgres.fields import JSONField
class Category(models.Model):
title = models.CharField(max_length = 200)
def __str__(self):
return self.title
class SubCategory(models.Model):
title = models.CharField(max_length = 200)
category = models.ForeignKey(Category)
def __str__(self):
return self.title
class Product(models.Model):
title = models.CharField(max_length = 200)
category = models.ForeignKey(Category)
subcategory = models.ForeignKey(SubCategory)
description = models.TextField(blank = True)
attributes = JSONField()
date = models.DateTimeField('дата добавления')
def __str__(self):
return self.title
这是我的models.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Product, Category, SubCategory
def electronic_micro(request):
micro_all = Product.objects.filter(category__title__icontains="Микросхемы")
return render(request,'electronic/micro_all.html', {'micro_all': micro_all})
def electronic_base(request):
return render (request, "electronic/electronic_base.html")
这是我的看法