Package: django-canonical-amp
django-canonical-ampA small set of tools meant to make building canonicalAMPpages withDjangoa little easier.InstallationTo enable the functionality of this package you need to add it to various keys of your project settings (settings.py per default) just as with other Django apps.INSTALLED_APPSdjango-canonical-amp needs to be added to your installed apps.INSTALLED_APPS=[# ...'amp',]TEMPLATESAdditionally you need to update your template settings to use the django-canonical-amp backend which is just a wrapper around Django's default backend.TEMPLATES=[{'BACKEND':'amp.template.backends.amp.AmpTemplates',# ...},]MIDDLEWAREThis step is optional but if you want to serveserver-side-rendered AMPstraight from your Django app you can add the transformer middleware to your stack.Important:this is experimental. The transformations happen by using a shared library built from the Go implementation of theamppackager. This package ships with compiled versions forlinux/amd64anddarwin/amd64operating systems.If you want to use this middleware you also need to make sure your production system is able to perform requests tohttps://cdn.ampproject.org/rtv/metadatafor the transformer to be able to fetch the current runtime version.MIDDLEWARE=[# ...'amp.middleware.AmpOptimizerMiddleware',]This middleware needs to execute before Django'sdjango.middleware.gzip.GZipMiddlewarefor the transformer to be able to alter the response.If you make use of this middleware you can additionally setAMP_REWRITE_URLSin your settings toFalse. By doing so you instruct the transformer to leave your URLs alone and don't rewrite them to a AMP cache URL - this comes with pros and cons: delivery times might be better from the cache though there's no guarantee your content (and assets) are already available from the cache. This settings defaults tonot DEBUGto don't rewrite URLs for all non-production environments.AMP components relying on API endpoints served by your application need them to be served according toAMP's CORS specification. django-canonical-amp ships with another middleware that adds the required headers. For it to work make sure your server is able to accesshttps://cdn.ampproject.org/caches.json. Then add the middleware to your stack:MIDDLEWARE=[# ...'amp.middleware.AmpCorsMiddleware',]UsageThe basic functionality of the package are two template tags that are available after installation. They make it able to dynamically define usedAMP components. For them to and up in the<head>of your HTML make sure you add the{% amp.components %}tag like so:{% load amp %}<!DOCTYPE html><html⚡lang="de"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width,minimum-scale=1"><metaname="description"content="{{ description }}"><linkrel="preload"as="script"href="https://cdn.ampproject.org/v0.js"><scriptasyncsrc="https://cdn.ampproject.org/v0.js"></script>{% amp.components %}<!-- This will expand to a list of <script> tags:<script async custom-element="amp-..." src="https://cdn.ampproject.org/v0/amp-...-0.1.js"></script>--><styleamp-custom>{%blockstyle%}{%endblock%}</style>Somewhere else in your templates you can then define dependencies by calling{% amp.require_component "<component>" "<version>" %}like in the following example:{% if youtube_id %}
{% amp.require_component "amp-youtube" %}<amp-youtubedata-videoid="{{ youtube_id }}"layout="responsive"width="480"height="270"></amp-youtube>{% endif %}Ifyoutube_idevaluates toFalsein the above example theamp-youtubeextension would not be injected to your page and it would stay AMP valid.The version can be omitted. It will then default to 0.1.