简体   繁体   English

在Django Mezzanine中,如何防止在page_processor上缓存?

[英]In Django Mezzanine, how to I prevent caching on a page_processor?

With the following I created a Page that handles a post request. 通过以下内容,我创建了一个用于处理发布请求的页面。

Mezzanine 4.2.3 Django 1.10.8 Python 2.7.10 PostgreSQL 9.6.3 Darwin 17.4.0 夹层4.2.3 Django 1.10.8 Python 2.7.10 PostgreSQL 9.6.3达尔文17.4.0

Here is my models.py for the Page. 这是我的专页的models.py

from mezzanine.pages.models import Page
from django.db import models

class MyCustomPage(Page):
    a_field = models.CharField(max_length=100)

Here is my page_processors.py for the Page. 这是我的Page的page_processors.py

from django.views.decorators.cache import never_cache
from django.contrib import messages
from .forms import MyCustomForm
from .utils import do_something_specific
from .models import MyCustomPage
from mezzanine.pages.page_processors import processor_for
from django.shortcuts import redirect


@never_cache
@processor_for(MyCustomPage)
def my_custom_page(request, page):

    if request.method == "POST":
        form = MyCustomForm(request.POST)

        if form.is_valid():
            do_something_specific(form.cleaned_data)
            messages.success(request, "Your post request was a success!")
            return redirect(page.get_absolute_url())

        else:
            return {
                "form": form,
            }

    else:  # is GET request
        return {
            "form": MyCustomForm(),
        }

The @never_cache decorator seems to prevent the python code from being cached, however the template is being cached. @never_cache装饰器似乎阻止python代码被缓存,但是模板被缓存。 Meaning, if I post to the url, it will call do_something_specific(form.cleaned_data) and that will happen, and it even seems to set the messages.success . 意思是,如果我发布到url,它将调用do_something_specific(form.cleaned_data) ,并且会发生这种情况,甚至似乎设置了messages.success But then when I do a regular get request the next time on the page, it will use the cached template and the messages.success message will be there. 但是,当我下次在页面上进行常规的get请求时,它将使用缓存的模板,并且messages.success消息就会出现。

For what it is worth, I am using redis as my caching backend. 对于它的价值,我使用redis作为我的缓存后端。 I was using memcached but got the same exact results. 我使用的是memcached,但得到的结果完全相同。 I use the caching for a different part of my application. 我将缓存用于应用程序的不同部分。 Also, I'm pretty familiar with caching. 另外,我对缓存非常熟悉。 I use it a lot in all of my applications. 我在所有应用程序中都经常使用它。 This seems to be something that is related to mezzanines caching. 这似乎与Mezzanines缓存有关。

Ideally I'd just like to completely disable caching for mezzanine and ONLY cache what I explicitly tell it to cache, nothing else. 理想情况下,我只想完全禁用夹层的缓存,并且仅缓存我明确告诉其进行缓存的内容,没有别的。

Update: 更新:

@iamkhush Yes, using redis-server monitor I can verify the key is the same. @iamkhush是的,使用redis-server monitor我可以验证密钥是否相同。 Here is what I do. 这是我的工作。 Clear cache. 清除缓存。 Runserver. RUNSERVER。 GET the URL in browser. 在浏览器中获取URL。 Cache does not exist so it renders the template and SETs it in cache. 缓存不存在,因此它将呈现模板并将其设置在缓存中。 Then, I fill out the form and POST. 然后,我填写表格并发布。 the page_processor runs all of the code in my "if POST" block. page_processor运行我的“ if POST”块中的所有代码。 Then it goes to the cache and GETs with the key, which exists. 然后,它进入缓存并使用存在的密钥进行GET。 So instead of re-rendering template, it just gets whatever is in cache so the success message doesn't get into the template. 因此,它无需重新渲染模板,而是获取缓存中的所有内容,因此成功消息不会进入模板。 I refresh a few times, it GETs same key. 我刷新几次,它获取了相同的密钥。 So I clear cache, run server, visit url, no cache so template is rendered and it picks up the success message(s), that html is then SET in cache. 因此,我清除了缓存,运行服务器,访问了url,没有缓存,因此呈现了模板,并且它接收到成功消息,然后将html设置在缓存中。 If I refresh(re-GET) the url, it will retrieve the template from cache. 如果刷新(重新获取)URL,它将从缓存中检索模板。

I think this is because the result of the middlewares mezzanine.core.middleware.FetchFromCacheMiddleware and mezzanine.core.middleware.UpdateCacheMiddleware 我认为这是因为中间件mezzanine.core.middleware.FetchFromCacheMiddlewaremezzanine.core.middleware.UpdateCacheMiddleware

Check about it here - http://mezzanine.jupo.org/docs/caching-strategy.html#cache-middleware . 在此处进行检查-http: //mezzanine.jupo.org/docs/caching-strategy.html#cache-middleware

You should remove these to get the desired result 您应该删除这些以获得预期的结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM