问题是如何让redis json数据显示在我的模板中?因为某种原因我不能让它工作。任何帮助都将不胜感激。。在
这是我目前所拥有的,我的json数据被正确地保存到redis中,但是我仍然不知道如何在我的模板中显示它。所以目标客户姓名我希望它来自redis并遍历我模板的json数据。在
def home(request):
"""Renders the home page."""
assert isinstance(request, HttpRequest)
#obj = Customer.objects.all()
cust = Customer.objects.all()
da = serializers.serialize('json', cust, fields=('customer_id', 'customer_name'))
r_conn = redis.StrictRedis(host='172.16.1.98', port=6379, db=1)
r_conn.set('dimcert_cust', da)
cust_cache = r_conn.get('dimcert_cust')
obj = json.load(cust_cache)
return render(
request,
'app/index.html',
{
'title':'Home Page',
'year':datetime.now().year,
'obj': obj,
}
)
from django.db import models
import json
class Customer(models.Model):
objects = models.Manager()
customer_id = models.IntegerField()
customer_name = models.CharField(max_length=255)
created_at = models.DateField
updated_at = models.DateField
通过执行
obj = json.load(cust_cache)
将json转换为字典,因此在模板中,您应该具有如下内容:作为脚注,我可以指出redis hashes是存储数据的更好方法。在
相关问题 更多 >
编程相关推荐