Blog の
include 化して
README.md
を
結論からsekizai
の
[TOC]
前提
以下の
OS
CentOS release 6.9 (Final)Python Version
Python 2.7.8Package (必要そうな
ものだけ 抜粋)
Django (1.10.7) Mezzanine (4.2.3)
インストール
インストール、
pip 実行
pip install django-ads ---------------------------------- Successfully built django-ads Installing collected packages: django-classy-tags, django-sekizai, django-ads Successfully installed django-ads-0.1.8 django-classy-tags-0.8.0 django-sekizai-0.10.0 ----------------------------------
django-classy-tags
、django-sekizai
という ライブラリが 一緒に インストールされました。
あまり知らない ライブラリですが、 どちらも tempalte 関連の Utility Library のようです。 settings.py に
アプリケーションを 追加
アプリケーションads
を追加します。 INSTALLED_APPS = ( ..... "ads", )
Migration を
実行
Migration コマンドを実行します。 どんなpython2.7 manage.py migrate ads --------------------------------- Operations to perform: Apply all migrations: ads Running migrations: Applying ads.0001_initial... OK Applying ads.0002_adimage... OK Applying ads.0003_auto_20170207_1034... OK Applying ads.0004_remove_ad_image... OK ---------------------------------
テーブルが できたのか 確認します。 予想外にpython2.7 manage.py dbshell -------------------------------------------------------------------------- \d スキーマ | 名前 | 型 | 所有者 ----------+-------------------------------------+------------+------------ public | ads_ad | テーブル | xxxxxxxxx public | ads_ad_id_seq | シーケンス | xxxxxxxxx public | ads_adimage | テーブル | xxxxxxxxx public | ads_adimage_id_seq | シーケンス | xxxxxxxxx public | ads_advertiser | テーブル | xxxxxxxxx public | ads_advertiser_id_seq | シーケンス | xxxxxxxxx public | ads_category | テーブル | xxxxxxxxx public | ads_category_id_seq | シーケンス | xxxxxxxxx public | ads_click | テーブル | xxxxxxxxx public | ads_click_id_seq | シーケンス | xxxxxxxxx public | ads_impression | テーブル | xxxxxxxxx public | ads_impression_id_seq | シーケンス | xxxxxxxxx
管理系の テーブルが できています。 settings.py の
TEMPLATES の context_processors を 編集する
私 のsettings.py には 既に いたので 追加していませんが、 なければ context_processors に django.template.context_processors.request
を追加します。 'OPTIONS': { 'context_processors': [ ... 'django.template.context_processors.request', ... ],
settings.py の
MIDDLEWARE を 編集する
これもsettings.py には 既に いましたが、 なければ MIDDLEWARE に django.contrib.sessions.middleware.SessionMiddleware
を追加します。 MIDDLEWARE = [ ... 'django.contrib.sessions.middleware.SessionMiddleware', ... ]
広告の 設定
この Blog には
Blog 記事の
上下に 表示する 広告 AMP ページの
画面下部に 表示する 広告
razisayyed/django-ads: Ads Management System for Django Framework はamp-sticky-ad
タグには
Blog に
設定していた 広告タグ
以下のようなタグを 設定していました。 <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-xxxxxxxxxxxxxxxx" data-ad-slot="xxxxxxxxx" data-ad-format="auto"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
settings.py を
編集
上記の広告タグは、 settings.py 上で 以下のように 記載できます。 ADS_GOOGLE_ADSENSE_CLIENT = 'ca-pub-xxxxxxxxxxxxxxxx' # OPTIONAL - DEFAULT TO None ADS_ZONES = { 'blog_ads_top_and_bottom': { 'name': _('Top And Bottom'), 'google_adsense_slot': 'xxxxxxxxx', # OPTIONAL - DEFAULT TO None 'google_adsense_format': 'auto', # OPTIONAL - DEFAULT TO None }, }
sekizai_tags の
load
django-ads/render_ads_zone.html at master · razisayyed/django-ads の実装は 以下のようになっています。 sekizai の{% load sekizai_tags %}{% spaceless %} {% if ad %} {% for image in ad.images.all %} <a href="{% url 'ads:ad-click' ad.id %}" target="_blank" rel="nofollow" title="{{ ad.title }}" class="django-ads-ad visible-{{ image.device }}"> <img src="{{ image.image.url }}" class="img-responsive" /> </a> {% endfor %} {% elif google_adsense_client and zone.google_adsense_slot %} {% addtoblock "js" %}<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>{% endaddtoblock %} <ins class="adsbygoogle" style="display:block" data-ad-client="{{ google_adsense_client }}" data-ad-slot="{{ zone.google_adsense_slot }}" data-ad-format="{{ zone.google_adsense_format|default:'auto' }}"></ins> <script>(adsbygoogle = window.adsbygoogle || []).push({});</script> {% endif %} {% endspaceless %}
addtoblock タグ で js ブログに ads の JavaScript が 差し込まれる 作りになっているので、 ` 規定テンプレートに、<wbr>差し込み<wbr>箇所となる<wbr>
` を追加します。 <head> {% load sekizai_tags %} {% render_block "js" %} </head>
タグが 差し込まれない
上記の
{% addtoblock "js" %}<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>{% endaddtoblock %}
{% autoescape off %} <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> {% endautoescape %}
最終的な 導入する / しない
sekizai タグで
以上です。
autoescape を
off に しないと 出力されませんでした。 原因は これな 気もします。 ↩
コメント