In your Wagtail, you sometimes may want to vary the template output depending on whether the page is being previewed or viewed live.

It might be as simple as adding a banner to clearly show that you are previewing a page or you might want to disable some tracking code during preview (Google Analytics for example).

Wagtail adds a variable is_preview to the request you can use to check this:

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.

So, in your template, you can do:

{% if not request.is_preview %}
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      ...
    </script>
{% endif %}

Related Posts

  • Defining custom settings in Wagtail
  • Making publish the default action in Wagtail
  • Migrating your Wagtail site to a different database engine
  • Programatically creating redirects in Wagtail
  • Change the Wagtail site domain via a management command