[Fixed]-Accelerated mobile pages in django?

6👍

I am a fellow django AMP developer but definitely not an expert, we are using the following url pattern

url(r'^api/news/', include('news.api_urls'), {"type", "regular"}), # regular

url(r'^api/amp/news/', include('news.api_urls'), {"type": "amp"}), # AMP

and in the view generating different context to be passed to the templates, canonicals in the templates point to each other and it seems to work

4👍

In the view, you can set the template variable in a number of different ways, like in a GET query parameter:

if request.GET.get('amp', 0) == 1:
    template_name = "amp.html"
else:
    template_name = "regular.html"

You can pass template_name as variable into the context, which you can then use when rendering the page:

{% extends template_name %}

That will allow you to render two entirely different layouts using the same view code/urls/context.

4👍

I create app https://github.com/shtalinberg/django-amp-tools for this

{% load amp_tags %} in template

{% amp_canonical_link request %} in meta

and create “amp” folder in templates

it’s first release. more documentation and futures will be added

1👍

Just to improve the first answer, i am providing a more detailed example.

project/urls.py

url(r'', include('app.urls'), {"type": "regular"}),
url(r'^amp/', include('app.urls'), {"type": "amp"}),

app/urls.py

url(r'^home/$', views.home),    

views.py

def home(request,type):
    if(type=='amp'):
        return render(request, 'app/amp/page.html', {}) 
    else:
        return render(request, 'app/page.html', {}) 

Leave a comment