#django #python #wagtail

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:

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