Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django中以编程方式渲染和缓存视图?_Python_Django_Django Templates_Django Views_Django Cache - Fatal编程技术网

Python 如何在Django中以编程方式渲染和缓存视图?

Python 如何在Django中以编程方式渲染和缓存视图?,python,django,django-templates,django-views,django-cache,Python,Django,Django Templates,Django Views,Django Cache,我在Django有一个视图,渲染速度非常慢。我想以编程的方式呈现这个视图并缓存它,但还没有弄明白怎么做。有没有简单的方法可以调用我的StatusView并将标记作为字符串获取,以便缓存它 以下是我对缓存装饰器的看法: class StatusView(ListView): template_name = 'network/list.htm' context_object_name = 'network' def get_queryset(self): r

我在Django有一个视图,渲染速度非常慢。我想以编程的方式呈现这个视图并缓存它,但还没有弄明白怎么做。有没有简单的方法可以调用我的
StatusView
并将标记作为字符串获取,以便缓存它

以下是我对缓存装饰器的看法:

class StatusView(ListView):
    template_name = 'network/list.htm'
    context_object_name = 'network'

    def get_queryset(self):
        return Network.objects.filter(date__lte=date.today()).order_by('-id')

    def get_context_data(self, **kwargs):
        context = super(StatusView, self).get_context_data(**kwargs)
        ...
        ...
        return context

    @method_decorator(cache_page(60 * 1))
    def dispatch(self, *args, **kwargs):
        return super(StatusView, self).dispatch(*args, **kwargs)

半路。我已经设法以编程方式呈现视图。现在我只需要缓存它


str(StatusView(request=request).get(request.render())

我的Django技能已经不太熟练了,所以我花了一点功夫,但我得到了:

from django.middleware.cache import UpdateCacheMiddleware
from django.utils.cache import learn_cache_key
from django.http import HttpRequest
from network.views import StatusView

request = HttpRequest()
request.META['SERVER_NAME'] = '1.0.0.127.in-addr.arpa' # important
request.META['SERVER_PORT'] = '8000'                   # important
request._cache_update_cache = True
response = StatusView(request=request).get(request)
cacher = UpdateCacheMiddleware()
cacher.process_response(request, response).render()

这不是你在用
cache\u页面
decorator做的吗?对不起,我应该说得更详细一些。我有一个后台作业,我想从中定期调用视图并缓存渲染结果。当从浏览器请求时,视图需要很长时间才能呈现,并且内容只有在后台作业运行时才会更改,因此以编程方式生成和缓存视图会有所帮助。我也有类似的问题,希望实现同样的效果。你的想法很有趣。为什么request.META['SERVER\u NAME']很重要?你是怎么想出这个解决方案的?您是否找到了一个描述这一点的教程?