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
Here is my models.py
for the Page.
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.
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. 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
. 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.
For what it is worth, I am using redis as my caching backend. I was using memcached but got the same exact results. 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.
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. Here is what I do. Clear cache. Runserver. GET the URL in browser. 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. Then it goes to the cache and GETs with the key, which exists. 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. If I refresh(re-GET) the url, it will retrieve the template from cache.
I think this is because the result of the middlewares mezzanine.core.middleware.FetchFromCacheMiddleware
and mezzanine.core.middleware.UpdateCacheMiddleware
Check about it here - http://mezzanine.jupo.org/docs/caching-strategy.html#cache-middleware.
You should remove these to get the desired result