Accelerated mobile pages in django?
Asked Answered
D

4

13

Are any Django developers working on building out AMP-HTML pages?

(For reference: Google AMP-project)

If I understand it correctly, the way AMP-HTML works is, you create 2 separate files for each page. The normal HTML result, plus a new AMP-HTML file. The AMP-HTML file is a slimmed-down version of the normal HTML page, and is used by Google to return an accelerated mobile page (hence the name, "Accelerated Mobile Pages"). Each of these files then reference each other in a tag in the head, which tells Google to use the AMP file for mobile results.

Within the Django framework, I'm wondering how to create 2 separate files based on the same set of content.

How might it be possible to make use of a single context that could be passed along to two HTML results? One result being the normal HTML page, the other being an AMP-HTML page.

In Django a URL must be created for each page to be returned. How might a second URL be automatically generated for each existing URL? Additionally, how can a single context be called up for each of those URLs?

Could it be done in the view perhaps? Such as, returning an .html extension which is the AMP page, and then also rendering the template for the URL?

I don't have any answers yet on how to tackle this. Looking for feedback and suggestions. It appears this is the first-ever question posted to stackoverflow on Django and AMP-HTML. It is likely to not be the last.

Deliver answered 29/1, 2016 at 18:30 Comment(1)
Maybe you could use a middleware to detect which template to use, i would need to check the amp-html project to see how it is supposed to work,Postwar
D
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

Deadman answered 18/2, 2016 at 8:35 Comment(2)
Couldn't you just use a single url as in: url(r'^api/(:?(?P<type>amp)/)?news/ and the view would have a type='regular' keyword argument so that when the regex matches the type group you end up passing type='amp' and when it doesn't match the view gets type='regular'.Drachm
@Drachm that seems like a more efficient way of doing it, thanks for the suggestionDeadman
A
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.

Anticathexis answered 23/2, 2016 at 4:41 Comment(0)
H
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

Hypodermis answered 14/12, 2016 at 12:49 Comment(0)
H
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', {}) 
Heyward answered 4/5, 2018 at 2:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.