django数据不会动态变化

时间:2015-08-31 12:06:36

标签: python django mongodb

我写了一个django应用程序来显示在html模板中从mongodb获取的一些数据。所有订单商品ID均以状态'APPROVED'

获取

models.py:

from pymongo import MongoClient

class GetNewOrders(object):
    def __init__(self):
        self.client = MongoClient('localhost',27017)
        self.db = self.client['Flipkart']
        self.sale_order = list(self.db['sale_order'].find({'status':'APPROVED'}))

    def getOrderItemId(self):
        oiids = []
        for each in self.sale_order:
            oiids.append(each['orderItemId'])
        return oiids

views.py:

from django.shortcuts import render
from django.http import HttpResponse
from orders.models import GetNewOrders

no = GetNewOrders()
oiids_new = no.getOrderItemId()

def orders(request):
    context_dict = {'oiids_new':oiids_new}
    return render(request, 'orders/orders.html', context_dict)

我在html文件中使用for循环来显示该数据。如果我将文档的状态更改为'APPROVED',则在重新启动服务器之前,它不会反映在我的html中。如何在不重启django服务器的情况下在html中显示已更改的数据?

1 个答案:

答案 0 :(得分:5)

因为GetNewOrders处于模块级别,它只执行一次 - 您需要将其移动到函数体中:

def orders(request):
    no = GetNewOrders()
    oiids_new = no.getOrderItemId()

    context_dict = {'oiids_new':oiids_new}
    return render(request, 'orders/orders.html', context_dict)