You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Django Mezzanine中如何阻止page_processor缓存?含模板缓存异常

Fixing Page Processor Caching in Django Mezzanine

Got it, let's work through this caching issue you're facing. You added @never_cache to your page processor, but after submitting a POST request, even though the backend logic runs and the success message is set, the template still shows stale content. Here's how to fully disable caching for your page processor in Mezzanine:

1. Double Down on Cache Control Decorators

Mezzanine has its own layer of page caching that might override Django's basic @never_cache alone. Combine it with more explicit cache control headers to leave no room for caching:

from django.views.decorators.cache import never_cache, cache_control

@never_cache
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)
def your_page_processor(request, page):
    # Your existing POST handling logic here
    if request.method == "POST":
        form = YourForm(request.POST)
        if form.is_valid():
            do_something_specific(form.cleaned_data)
            messages.success(request, "Action completed successfully!")
    
    # Return your context as usual
    return {"form": form, ...}

The cache_control decorator adds strict headers that tell both the server and browsers not to cache the response.

2. Manually Set Cache Headers in the Response (If Needed)

If the decorators still aren't cutting it, you can directly modify the response headers when returning from the processor. Instead of just returning a context dict, return a full HttpResponse with explicit cache rules:

from django.shortcuts import render
from django.views.decorators.cache import never_cache

@never_cache
def your_page_processor(request, page):
    context = {}
    # Your POST handling logic here
    
    # Render the template and set cache headers
    response = render(request, "your_template.html", context)
    response["Cache-Control"] = "no-cache, no-store, must-revalidate"
    response["Pragma"] = "no-cache"
    response["Expires"] = "0"
    return response

This bypasses any Mezzanine-level caching that might be interfering.

3. Check for Template-Level Caching

Mezzanine templates often use Django's {% cache %} tag for performance. If your template has a cache block wrapping dynamic content, it will ignore processor-level cache settings. Either remove the cache tag entirely, or adjust its key to include variables that change on POST (like request method or session data):

<!-- Bad: Static cache key that won't invalidate on POST -->
{% cache 3600 page_content %}
    {{ dynamic_content }}
{% endcache %}

<!-- Good: Cache key that changes with request method/user -->
{% cache 0 page_content request.method request.user.id %}
    {{ dynamic_content }}
    {% if messages %}
        {% for message in messages %}
            <div class="alert">{{ message }}</div>
        {% endfor %}
    {% endif %}
{% endcache %}

Using 0 as the timeout ensures the cache never persists, and adding request.method guarantees the cache refreshes when a POST is submitted.

4. Refresh the Page Object (If Mezzanine Caches It)

Mezzanine might cache the Page model instance passed to the processor. To ensure you're working with fresh data, re-fetch the page from the database:

from mezzanine.pages.models import Page

@never_cache
def your_page_processor(request, page):
    # Refresh the page object to avoid stale cached data
    page = Page.objects.get(pk=page.pk)
    # Rest of your logic...

5. Verify Browser Cache

Sometimes the issue is on the client side. Clear your browser's cache, or use incognito mode to test—browsers often cache GET responses aggressively, even if the server sends no-cache headers.

By combining these steps, you should eliminate all caching layers that are causing your template to show stale content after a POST request.

内容的提问来源于stack exchange,提问作者teewuane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 06:56:35