====================== コメントフレームワーク ====================== :revision-up-to: 17812 (1.4) .. module:: django.contrib.comments :synopsis: Django のコメントフレームワークです。 .. highlightlang:: html+django Django には単純ながらカスタマイズ可能なコメントフレームワークが付属していま す。この組み込みのコメントフレームワークを使えば、任意のモデルにコメントを 付加できます。つまり、ブログエントリや写真、本、本の各章、その他あらゆるも のにコメントを付けられるのです。 .. note:: Django の古い (ドキュメント化されなかった) コメントフレームワークを使っ ていたのなら、アップグレードが必要です。 :doc:`アップグレードガイド ` を参照してください。 クイックスタートガイド ====================== ``comments`` を使うには、以下のステップに従います: #. :setting:`INSTALLED_APPS` に ``'django.contrib.comments'`` を インストールします。 #. ``manage.py syncdb`` を実行して、コメントフレームワークのテーブルを 生成します。 #. コメントアプリケーションの URL をプロジェクトの ``urls.py`` にマップ します: .. code-block:: python urlpatterns = patterns('', ... (r'^comments/', include('django.contrib.comments.urls')), ... ) #. :ref:`コメントテンプレートタグ ` を使って、 コメント機能をテンプレートに埋め込みます。 :doc:`/ref/contrib/comments/settings` も調べておきましょう。 .. _comment-template-tags: コメントテンプレートタグ ======================== コメントシステムは、主にテンプレートタグを使って操作します。テンプレートタ グを使えば、コメントをポストするためのフォームや、ポストされたコメントを埋 め込めます。 他のカスタムテンプレートタグライブラリと同様、使うには :ref:`カスタムタグライブラリをロード ` しておかねばなりません:: {% load comments %} タグライブラリをロードすれば、後で解説するテンプレートタグを使えるようにな ります。 どのオブジェクトにコメントを結びつけるか指定する ------------------------------------------------ Django のコメントは、全て何らかのオブジェクトに「結びついて (attached)」い ます。コメントは、任意の Django モデルのインスタンスに結びつけられます。 以下のタグを使うと、 2 種類の方法で、コメントを結びつけたいオブジェクトを指 定できます: #. オブジェクトを直接参照する方法 -- より一般的な方法です。たいていの場 合、コメントを結びつけたいオブジェクトはテンプレートコンテキストの中 に入っているはずです。そのオブジェクトを使います。 例えば、あるブログのエントリ表示ページに ``entry`` という変数が入っ ていて、エントリオブジェクトを表現しているなら、以下のようにして、 ``entry`` に結び付いたコメントの数を表示できます:: {% get_comment_count for entry as comment_count %}. #. オブジェクトをコンテンツタイプとオブジェクト id で指定する方法。何ら かの理由で、コメントを結び付ける対象のオブジェクトに直接アクセスでき ない場合に使います。 上のような例で、オブジェクトの id が ``14`` だと分かっていて、かつ対 象のオブジェクトにアクセスできないなら、以下のようにします:: {% get_comment_count for blog.entry 14 as comment_count %} この例の ``blog.entry`` は、アプリケーションラベルと (小文字にした) モデルクラス名をつなげたものです。 コメントを表示する ------------------ .. To display a list of comments, you can use the template tags :ttag:`render_comment_list` or :ttag:`get_comment_list`. コメントの一覧を表示するには、テンプレートタグ :ttag:`render_comment_list` または :ttag:`get_comment_list` を使用します。 .. templatetag:: render_comment_list .. Quickly rendering a comment list ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 手軽にコメント一覧をレンダリングする ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. The easiest way to display a list of comments for some object is by using :ttag:`render_comment_list`:: あるオブジェクトへのコメントを一覧表示する一番簡単な方法は :ttag:`render_comment_list` を使用する方法です:: {% render_comment_list for [object] %} .. For example:: たとえば:: {% render_comment_list for event %} .. This will render comments using a template named ``comments/list.html``, a default version of which is included with Django. これは ``comments/list.html`` という名前のテンプレートを使ってコメントを レンダリングします。このテンプレートのデフォルトバージョンは Django に標準で 含まれています。 .. templatetag:: get_comment_list .. Rendering a custom comment list ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 独自のコメントリストをレンダリングする ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. To get the list of comments for some object, use :ttag:`get_comment_list`:: あるオブジェクトへのコメントのリストを取得するには :ttag:`get_comment_list` を使います:: {% get_comment_list for [object] as [varname] %} 例を挙げましょう:: {% get_comment_list for event as comment_list %} {% for comment in comment_list %} ... {% endfor %} .. This returns a list of :class:`~django.contrib.comments.models.Comment` objects; see :doc:`the comment model documentation ` for details. これは :class:`~django.contrib.comments.models.Comment` オブジェクトのリストを 返します。詳細は :doc:`コメントモデルのドキュメント ` を参照してください。 .. templatetag:: get_comment_permalink .. Linking to comments ------------------- コメントにリンクする -------------------- .. versionadded:: 1.2 .. To provide a permalink to a specific comment, use :ttag:`get_comment_permalink`:: 特定のコメントへのパーマリンク (permalink) を提供するには、 :ttag:`get_comment_permalink` を使います:: {% get_comment_permalink comment_obj [format_string] %} .. By default, the named anchor that will be appended to the URL will be the letter 'c' followed by the comment id, for example 'c82'. You may specify a custom format string if you wish to override this behavior:: デフォルトでは URL の後ろに追加される名前付きアンカー (訳注: # に続けて記す フラグメント識別子) は、 'c82' のように、 'c' にコメント ID を続けたものに なります。この動作を変更したい場合、カスタム書式を指定することもできます:: {% get_comment_permalink comment "#c%(id)s-by-%(user_name)s"%} .. The format string is a standard python format string. Valid mapping keys include any attributes of the comment object. この書式を指定する文字列は、 Python 標準の書式化用文字列です。有効なマップキー として、コメントオブジェクトの属性すべてが利用可能になっています。 .. Regardless of whether you specify a custom anchor pattern, you must supply a matching named anchor at a suitable place in your template. 独自にカスタムしたアンカーのパターンを指定するかどうかに関わらず、 テンプレートの適切な場所に対応した名前付きアンカーを配置する必要があります。 .. For example:: たとえば:: {% for comment in comment_list %} permalink for comment #{{ forloop.counter }} ... {% endfor %} .. .. warning:: .. There's a known bug in Safari/Webkit which causes the named anchor to be forgotten following a redirect. The practical impact for comments is that the Safari/webkit browsers will arrive at the correct page but will not scroll to the named anchor. .. warning:: Safari/Webkig にはリダイレクト後に名前付きアンカーが忘れられる既知のバグが あります。これが実際問題としてコメントに与える影響は、 Safari/webkig ブラウザは正しいページにたどり着くものの名前付きアンカーの位置にまで スクロールしてくれない、というものです。 .. templatetag:: get_comment_count コメントの数を数える -------------------- オブジェクトに結びつけられたコメントの数を取得するには、 :ttag:`get_comment_count` を使います:: {% get_comment_count for [object] as [varname] %} 例を挙げましょう:: {% get_comment_count for event as comment_count %}

This event has {{ comment_count }} comments.

コメント投稿フォームを表示する ------------------------------ ユーザがコメントを投稿できるようフォームを表示するには、 :ttag:`render_comment_form` や :ttag:`get_comment_form` を使います。 .. templatetag:: render_comment_form コメントフォームを手軽に出力する ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ コメントフォームを表示する最も簡単な方法は、 :ttag:`render_comment_form` を使うやり方です:: {% render_comment_form for [object] %} 例を挙げましょう:: {% render_comment_form for event %} このコードによって、Django に付属のデフォルトの ``comments/form.html`` テン プレートを使ってコメントフォームがレンダされます。 .. templatetag:: get_comment_form カスタムのコメントフォームを出力する ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ コメントフォームのルック&フィールを制御したければ、 :ttag:`get_comment_form` を使って :doc:`フォームオブジェクト ` を取得し、テンプレート内で使います:: {% get_comment_form for [object] as [varname] %} これで、フォームは以下のように書けます:: {% get_comment_form for event as form %} {% csrf_token %} {{ form }}
このアプローチをとるときには、いくつか考慮すべき点があります。後述の `コメントフォームに関する注意 <#notes-on-the-comment-form>`_ をよく読んでく ださい。 .. templatetag:: comment_form_target コメントフォームターゲットを取得する ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 上の例では、 :ttag:`comment_form_target` というもう一つのタグを使って、フォー ムの ``action`` 属性を取得していたことに気づいたでしょうか。このタグは、常 にコメントフォームのポスト先として正しい URL を返します。コメントフォームを 使うときには、いつも以下のように書いてください::
.. Redirecting after the comment post ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ コメント投稿後にリダイレクトする ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. To specify the URL you want to redirect to after the comment has been posted, you can include a hidden form input called ``next`` in your comment form. For example:: コメントが投稿された後にリダイレクトする URL を指定したい場合、 ``next`` という名前の非表示の input 要素をフォームに含めることができます。たとえば:: .. _notes-on-the-comment-form: コメントフォームに関する注意 ---------------------------- コメントシステムの使っているフォームには、スパムを防ぐための重要な属性があ ります。これらの属性についてよく知っておきましょう: * フォームには、タイムスタンプやコメントを結び付ける対象のオブジェクト、 これらの情報を検証するための「セキュリティハッシュ(security hash)」な ど、いくつもの隠しフィールドが入っています。コメントスパム屋がよくや るように、これらのフィールドの値を改ざんしようとすると、コメントの投 稿に失敗します。 自作のコメントフォームを出力するときには、この隠しフィールドの値を変 更せずに渡してください。 * タイムスタンプは、長時間再生攻撃が繰り返されるのを防ぐために使われま す。フォームのリクエストからコメントのポストまで長時間経っている場合、 フォームの提出は拒否されます。 * コメントフォームには、「 `ハニーポット`_ (honeypot_)」フィールドがあ ります。このフィールドはトラップです: このフィールドに何らかのデータ が入力されると、そのコメントはスパムであると見なされます (スパムプロ グラムは、有効なフォーム入力を偽装するために、全てのフィールドを自動 的に埋めようとするからです)。 デフォルトのフォームは、 CSS を使ってこのフォームを隠し、ラベルにも 警告の文字列を入れます。カスタムのテンプレートでコメントフォームを使 うときにも、同じようにしてください。 .. The comments app also depends on the more general :doc:`Cross Site Request Forgery protection ` that comes with Django. As described in the documentation, it is best to use ``CsrfViewMiddleware``. However, if you are not using that, you will need to use the ``csrf_protect`` decorator on any views that include the comment form, in order for those views to be able to output the CSRF token and cookie. コメントアプリは Django に同梱されているより一般的な :doc:`CSRF 対策の機能 ` に依存しています。そちらのドキュメントに記載されている通り、 ``CsrfViewMiddleware`` を使うのがベストです。しかし、もしそれを使っていなければ、 CSRF トークンとクッキーを出力できるよう、コメントフォームを含むすべてのビューで ``csrf_protect`` デコレータを使う必要があります。 .. _honeypot: http://en.wikipedia.org/wiki/Honeypot_(computing) .. _ハニーポット: http://ja.wikipedia.org/wiki/%E3%83%8F%E3%83%8B%E3%83%BC%E3%83%9D%E3%83%83%E3%83%88 その他の情報 ============== .. toctree:: :maxdepth: 1 models settings signals upgrade custom forms moderation example