Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.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
我必须把我的Django中间件放在哪里才能缓存?_Django_Caching_Middleware - Fatal编程技术网

我必须把我的Django中间件放在哪里才能缓存?

我必须把我的Django中间件放在哪里才能缓存?,django,caching,middleware,Django,Caching,Middleware,我试图将缓存添加到Django项目中,因此我按照官方文档中的说明(据我所知)添加了缓存中间件,并获得了以下结果: MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.middleware.cache.UpdateCacheMiddleware', 'debug_toolbar.middleware.DebugToolbarMiddleware', 'django.contr

我试图将缓存添加到Django项目中,因此我按照官方文档中的说明(据我所知)添加了缓存中间件,并获得了以下结果:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
]
但这破坏了我的身份验证系统:在其他页面中,我为不同的身份验证级别获取不同的查询集,但如果我添加这些中间件,则会向所有用户显示相同的页面。 如何在我的项目中实现缓存,并且仍然能够为不同的用户提供不同的查询集

这是我在
views.py中的函数:

class ProgettoListView(generic.ListView, LoginRequiredMixin):
    model = Progetto
    template_name = 'main/list/progetto_list.html'

    def get_context_data(self, **kwargs):
        context = super(ProgettoListView, self).get_context_data(**kwargs)
        context['ore'] = []
        for progetto in Progetto.objects.all():
            ore_lavoro = Task.objects.filter(progetto=progetto).aggregate(sum_all=Sum('durata')).get('sum_all')
            if ore_lavoro is None:
                ore_lavoro = datetime.timedelta(0)
            context['ore'].append({'nome': progetto.nome, 'ore_lavorate': ore_lavoro,
                                   'percentuale': int(ore_lavoro / progetto.tempo_stimato * 100)})
        return context

    def get_queryset(self):
        if self.request.user.is_amm:
            return self.model.objects.all()
        return self.model.objects.filter(auts=self.request.user)
    path('pm/lista/progetti', login_required(views.ProgettoListView.as_view()), name='lista-progetti'),
只是一个boolan字段,
auts
是一个用户列表

下面是
url.py
中的一行:

class ProgettoListView(generic.ListView, LoginRequiredMixin):
    model = Progetto
    template_name = 'main/list/progetto_list.html'

    def get_context_data(self, **kwargs):
        context = super(ProgettoListView, self).get_context_data(**kwargs)
        context['ore'] = []
        for progetto in Progetto.objects.all():
            ore_lavoro = Task.objects.filter(progetto=progetto).aggregate(sum_all=Sum('durata')).get('sum_all')
            if ore_lavoro is None:
                ore_lavoro = datetime.timedelta(0)
            context['ore'].append({'nome': progetto.nome, 'ore_lavorate': ore_lavoro,
                                   'percentuale': int(ore_lavoro / progetto.tempo_stimato * 100)})
        return context

    def get_queryset(self):
        if self.request.user.is_amm:
            return self.model.objects.all()
        return self.model.objects.filter(auts=self.request.user)
    path('pm/lista/progetti', login_required(views.ProgettoListView.as_view()), name='lista-progetti'),

我遇到的问题是,is_amm设置为true的用户在访问列表页面时应该看到数据库中的所有项目,但这并没有发生。例如,如果我与该用户创建了一个新项目,他将无法在listview中看到该项目,尽管他可以通过直接URL访问该项目。

听起来您遇到的问题是,缓存视图时使用了错误的权限。我猜您已经将
cache\u页面
decorator本身添加到了视图中。这种方法的问题是,它将缓存对该特定URL的任何请求的响应。要允许URL具有多个缓存响应,至少有两个选项

  • 创建一个类似于(或包装)
    cache\u页面
    的新装饰器,以便装饰器检查用户的权限,然后获取相关的缓存响应
  • 不要使用
    cache\u页面
    并通过手动管理缓存

  • 就我个人而言,我会选择#2,因为它会更清晰一些,但我认为#1有可能更简洁。

    听起来您遇到的问题是缓存视图时使用了错误的权限。我猜您已经将
    cache\u页面
    decorator本身添加到了视图中。这种方法的问题是,它将缓存对该特定URL的任何请求的响应。要允许URL具有多个缓存响应,至少有两个选项

  • 创建一个类似于(或包装)
    cache\u页面
    的新装饰器,以便装饰器检查用户的权限,然后获取相关的缓存响应
  • 不要使用
    cache\u页面
    并通过手动管理缓存

  • 就我个人而言,我会选择#2,因为它会更清晰一些,但我认为#1有可能更简洁。

    您如何使用这些查询集?添加一些代码来显示问题所在,并突出显示您看到的问题所在。您如何使用这些查询集?添加一些代码以显示问题所在,并突出显示您看到的问题所在。