From ab45915a90949d0ad616615fc609cd50e02f450f Mon Sep 17 00:00:00 2001 From: Denis Voskvitsov Date: Tue, 27 Oct 2015 14:47:24 +0300 Subject: [PATCH] Set `request.is_preview` flag to show whether page is served as preview --- CHANGELOG.txt | 1 + docs/releases/1.3.rst | 1 + docs/topics/writing_templates.rst | 14 ++++++++++++++ wagtail/wagtailcore/models.py | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index f87ef55cf..8c5d03e86 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -23,6 +23,7 @@ Changelog * Added the ability to override the default manager on Page models * Added an optional human-friendly `site_name` field to sites (Timo Rieber) * Added success message after updating image from the image upload view (Christian Peters) + * Added a `request.is_preview` variable for templates to distinguish between previewing and live (Denis Voskvitsov) * New translations for Arabic and Latvian * Fix: Images and page revisions created by a user are no longer deleted when the user is deleted (Rich Atkinson) * Fix: HTTP cache purge now works again on Python 2 (Mitchel Cabuloy) diff --git a/docs/releases/1.3.rst b/docs/releases/1.3.rst index 022d25ac0..4a1bcb5ba 100644 --- a/docs/releases/1.3.rst +++ b/docs/releases/1.3.rst @@ -68,6 +68,7 @@ Minor features * Added the ability to override the default manager on Page models * Added an optional human-friendly ``site_name`` field to sites (Timo Rieber) * Added success message after updating image from the image upload view (Christian Peters) + * Added a ``request.is_preview`` variable for templates to distinguish between previewing and live (Denis Voskvitsov) * New translations for Arabic and Latvian diff --git a/docs/topics/writing_templates.rst b/docs/topics/writing_templates.rst index 2848a305b..c6b2c1cc9 100644 --- a/docs/topics/writing_templates.rst +++ b/docs/topics/writing_templates.rst @@ -212,3 +212,17 @@ By default the User Bar appears in the top right of the browser window, flush wi top:200px } + +Varying output between preview and live +======================================= + +Sometimes you may wish to vary the template output depending on whether the page is being previewed or viewed live. For example, if you have visitor tracking code such as Google Analytics in place on your site, it's a good idea to leave this out when previewing, so that editor activity doesn't appear in your analytics reports. Wagtail provides a ``request.is_preview`` variable to distinguish between preview and live: + +.. code-block:: html+django + + {% if not request.is_preview %} + + {% endif %} diff --git a/wagtail/wagtailcore/models.py b/wagtail/wagtailcore/models.py index 2c37d6ee6..9c0f16a1d 100644 --- a/wagtail/wagtailcore/models.py +++ b/wagtail/wagtailcore/models.py @@ -659,6 +659,8 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed return self.template def serve(self, request, *args, **kwargs): + request.is_preview = getattr(request, 'is_preview', False) + return TemplateResponse( request, self.get_template(request, *args, **kwargs), @@ -1193,6 +1195,8 @@ class Page(six.with_metaclass(PageBase, MP_Node, ClusterableModel, index.Indexed here - this ensures that request.user and other properties are set appropriately for the wagtail user bar to be displayed. This request will always be a GET. """ + request.is_preview = True + return self.serve(request) def get_cached_paths(self):