過去に、
Mezzanine AMP 対応の
上記テーマと
Device Handling — Mezzanine 4.2.3 documentation
現状の
Device Handling 機能が
削除される
Device Handling は、新しい バージョンで 削除されるようです。
Pagination not rendering (and possibly other inclusion tags) · Issue #1780 · stephenmcd/mezzanine同一 URL で、
AMP ページと 通常ページを 保持するのは あまり 一般的な 作りではなさそう
Google はレスポンシブ ウェブ デザイン を 推奨しています。
レスポンシブ ウェブ デザイン | Search | Google Developers
現状、この Blog の テーマの 切り替え方は 動的な 配信に 当たります。 説明を 読む限りは あまりお勧めされていないように 感じます。 1
動的な
そして、
参考
実装には、
* amp html - Accelerated mobile pages in django? - Stack Overflow
shtalinberg/django-amp-tools: Accelerated mobile pages (AMP) in django
blturner/django-amp: A small toolset for rendering Google AMP pages for Django projects
実装、 実施した こと
実装は、
django-amp-tools
は
実装する
* 通常ページ、
* middleware で
* settings.py に
* urls.py に、
MiddleWare の 作成
- middleware.py
process_request
で、URL に、 /amp/
を含む 場合は、 フラグを 設定しています。 from amp_start_blog_post import set_amp_detect class AMPDetectionMiddleware(object): def __init__(self, get_response=None): self.get_response = get_response # One-time configuration and initialization. def __call__(self, request): # Code to be executed for each request before # the view (and later middleware) are called. response = self.get_response(request) # Code to be executed for each request/response after # the view is called. return response def process_request(self, request): if '/amp/' in request.path: set_amp_detect(is_amp_detect=True, request=request) else: set_amp_detect(is_amp_detect=False, request=request)
ユーティリティメソッド
__init__.py
には、
これは、__init__.py
の
threadlocal で、
- __init__py
# -*- coding: utf-8 -*- import threading from amp_start_blog_post.settings import settings __version__ = '0.0.1' _local = threading.local() def set_amp_detect(is_amp_detect=False, request=None): request = request or getattr(_local, 'request', None) if request: request.is_amp_detect = is_amp_detect _local.is_amp_detect = is_amp_detect def get_amp_detect(request=None): is_amp_detect = False request = request or getattr(_local, 'request', None) # check if is_amp_detect is set on request if not is_amp_detect and hasattr(request, 'is_amp_detect'): is_amp_detect = request.is_amp_detect # if set out of a request-response cycle its stored on the thread local if not is_amp_detect: is_amp_detect = getattr(_local, 'is_amp_detect', False) return settings.AMP_TOOLS_TEMPLATE_FOLDER if is_amp_detect else u""
Loader の 作成
Loader も
記載が
Loader には、Loader
と、CachedLoader
が
AMP ページへの
Template のAMP_TOOLS_TEMPLATE_FOLDER
で、AMP_TOOLS_TEMPLATE_PREFIX
で
settings.py の 編集
Device Handling の
mezzanine Device Handling の
設定の 削除 ############################# # Device Settings ############################# #DEVICE_DEFAULT = "" #DEVICE_USER_AGENTS = ( # ("amp_start_blog_post", ("Android", "BlackBerry", "iPhone" # )), # )
loader の
変更 'loaders': [ ('amp_start_blog_post.loaders.CachedLoader', [ 'amp_start_blog_post.loaders.Loader', 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ]), ],
Middleware の
追加 "amp_start_blog_post.middleware.AMPDetectionMiddleware",
urls.py の 変更
mezzanine-blog の/amp
配下に
- urls.py
mezzanine.urls
の直上に、 blog アプリケーションの url を マッピングしました。 # ----------------------------- # Mezzanine's Blog app for AMP # ----------------------------- blog_installed = "mezzanine.blog" in settings.INSTALLED_APPS if blog_installed: BLOG_SLUG = settings.BLOG_SLUG.rstrip("/") if BLOG_SLUG: BLOG_SLUG += "/" blog_patterns = [ url("^amp/%s" % BLOG_SLUG, include("mezzanine.blog.urls")), ] urlpatterns += blog_patterns urlpatterns += [ url("^", include("mezzanine.urls")), # MOUNTING MEZZANINE UNDER A PREFIX # --------------------------------- # You can also mount all of Mezzanine's urlpatterns under a # URL prefix if desired. When doing this, you need to define the # ``SITE_PREFIX`` setting, which will contain the prefix. Eg: # SITE_PREFIX = "my/site/prefix" # For convenience, and to avoid repeating the prefix, use the # commented out pattern below (commenting out the one above of course) # which will make use of the ``SITE_PREFIX`` setting. Make sure to # add the import ``from django.conf import settings`` to the top # of this file as well. # Note that for any of the various homepage patterns above, you'll # need to use the ``SITE_PREFIX`` setting as well. # ("^%s/" % settings.SITE_PREFIX, include("mezzanine.urls")) ]
作成した python スクリプト
AMP の
kemsakurai/mezzanine-theme-amp-start-blog-post: mezzanine theme based by amp start
切り替えと
しばらくたったら、
以上です。
コメント