Skip to main content

Template tags and middlewares to build canonical AMP pages with Django easily.

Project description


A small set of tools meant to make building canonical AMP pages with Django a little easier.


To enable the functionality of this package you need to add it to various keys of your project settings ( per default) just as with other Django apps.


django-amp needs to be added to your installed apps.

  # ...


Additionally you need to update your template settings to use the django-amp backend which is just a wrapper around Django's default backend.

    'BACKEND': 'amp.template.backends.amp.AmpTemplates',
    # ...


This step is optional but if you want to serve server-side-rendered AMP straight 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 the amppackager. This package ships with compiled versions for linux/amd64 and darwin/amd64 operating systems.

If you want to use this middleware you also need to make sure your production system is able to perform requests to for the transformer to be able to fetch the current runtime version.

  # ...

This middleware needs to execute before Django's django.middleware.gzip.GZipMiddleware for the transformer to be able to alter the response.

If you make use of this middleware you can additionally set AMP_REWRITE_URLS in your settings to False. 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 to not DEBUG to 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 to AMP's CORS specification. django-amp ships with another middleware that adds the required headers. For it to work make sure your server is able to access Then add the middleware to your stack:

  # ...


The basic functionality of the package are two template tags that are available after installation. They make it able to dynamically define used AMP 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">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1">
    <meta name="description" content="{{ description }}">

    <link rel="preload" as="script" href="">
    <script async src=""></script>

    {% amp.components %}
    <!-- This will expand to a list of <script> tags:
    <script async custom-element="amp-..." src=""></script>

    <style amp-custom>
    {% block style %}
    {% endblock %}

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" %}
    data-videoid="{{ youtube_id }}"
    width="480" height="270"></amp-youtube>
{% endif %}

If youtube_id evaluates to False in the above example the amp-youtube extension 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.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-canonical-amp-0.1b0.tar.gz (4.7 MB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page