【保存版】Django 2.0以降で必須!`template.RequestContext`の代替テクニック


機能

RequestContext の主な機能は次のとおりです。

  • サイトの設定へのアクセスを提供します。
  • ログイン中のユーザー情報へのアクセスを提供します。
  • 現在のリクエストオブジェクトへのアクセスを提供します。

使い方

RequestContext を使用する方法は次のとおりです。

from django.template import RequestContext

# 現在のリクエストオブジェクトを取得
request = ...

# テンプレートを取得
template = loader.get_template('mytemplate.html')

# コンテキストを作成
context = RequestContext(request)

# コンテキスト変数を追加
context['my_variable'] = 'Hello, world!'

# テンプレートをレンダリング
rendered_html = template.render(context)

上記のコードでは、RequestContext クラスを使用して、現在のリクエストオブジェクトと my_variable という名前のコンテキスト変数をテンプレートコンテキストに追加しています。

利点

RequestContext を使用すると、テンプレートで次の利点が得られます。

  • テンプレートでサイトの設定に簡単にアクセスできる
  • コードが簡潔になる

注意点

Django 2.0 以降では、RequestContext は非推奨になりました。代わりに、django.template.context.make_context 関数を使用してコンテキストを作成することをお勧めします。

from django.template.context import make_context

# 現在のリクエストオブジェクトを取得
request = ...

# テンプレートを取得
template = loader.get_template('mytemplate.html')

# コンテキストを作成
context = make_context(request)

# コンテキスト変数を追加
context['my_variable'] = 'Hello, world!'

# テンプレートをレンダリング
rendered_html = template.render(context)

RequestContext を使用して、ログイン中のユーザーの名前を表示するテンプレートを作成する例を次に示します。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My Template</title>
</head>
<body>
  <h1>ようこそ、{{ user.username }} さん!</h1>
</body>
</html>

上記のテンプレートは、user という名前のコンテキスト変数に、ログイン中のユーザーの username 属性が含まれていることを前提としています。

  • django.template.Template.render メソッドは、テンプレートをレンダリングします。
  • django.template.Template クラスは、テンプレートオブジェクトを表します。
  • django.template.loader モジュールには、テンプレートファイルをロードするための関数とクラスが用意されています。
  • django.template.context_processors モジュールには、テンプレートコンテキストに自動的に追加されるコンテキスト変数を定義するコンテキストプロセッサが用意されています。


<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My Template</title>
</head>
<body>
  <h1>ようこそ、{{ user.username }} さん!</h1>
</body>
</html>
from django.http import HttpResponse
from django.shortcuts import render
from django.template import RequestContext
from django.contrib.auth.decorators import login_required

# ログイン必須のビュー
@login_required
def my_view(request):
  # 現在のリクエストオブジェクトを取得
  request = request

  # テンプレートを取得
  template = loader.get_template('mytemplate.html')

  # コンテキストを作成
  context = RequestContext(request)

  # テンプレートをレンダリング
  rendered_html = template.render(context)

  # レンダリングされた HTML をレスポンスとして返す
  return HttpResponse(rendered_html)

例2:サイトの設定にアクセスする

この例では、RequestContext を使用して、サイトの設定にアクセスするテンプレートを作成します。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My Template</title>
</head>
<body>
  <h1>サイト名: {{ settings.SITE_NAME }}</h1>
</body>
</html>
from django.http import HttpResponse
from django.shortcuts import render
from django.template import RequestContext
from django.conf import settings

def my_view(request):
  # 現在のリクエストオブジェクトを取得
  request = request

  # テンプレートを取得
  template = loader.get_template('mytemplate.html')

  # コンテキストを作成
  context = RequestContext(request)

  # テンプレートをレンダリング
  rendered_html = template.render(context)

  # レンダリングされた HTML をレスポンスとして返す
  return HttpResponse(rendered_html)

例3:テンプレートにリクエスト情報を渡す

この例では、RequestContext を使用して、テンプレートにリクエスト情報を渡します。

<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My Template</title>
</head>
<body>
  <h1>リクエストパス: {{ request.path }}</h1>
  <h1>リクエストメソッド: {{ request.method }}</h1>
</body>
</html>
from django.http import HttpResponse
from django.shortcuts import render
from django.template import RequestContext

def my_view(request):
  # 現在のリクエストオブジェクトを取得
  request = request

  # テンプレートを取得
  template = loader.get_template('mytemplate.html')

  # コンテキストを作成
  context = RequestContext(request)

  # テンプレートをレンダリング
  rendered_html = template.render(context)

  # レンダリングされた HTML をレスポンスとして返す
  return HttpResponse(rendered_html)
  • Django テンプレートエンジンには、他にも多くの機能があります。詳細については、Django のドキュメントを参照してください。
  • これらの例は、Django の基本的な使用方法のみを示しています。


django.template.context.make_context関数を使う

これが最も一般的で推奨される方法です。

from django.template.context import make_context

# 現在のリクエストオブジェクトを取得
request = ...

# テンプレートを取得
template = loader.get_template('mytemplate.html')

# コンテキストを作成
context = make_context(request)

# コンテキスト変数を追加
context['my_variable'] = 'Hello, world!'

# テンプレートをレンダリング
rendered_html = template.render(context)

django.shortcuts.render関数を使う

from django.shortcuts import render

# 現在のリクエストオブジェクトを取得
request = ...

# テンプレートとコンテキストを指定してレンダリング
rendered_html = render(request, 'mytemplate.html', {'my_variable': 'Hello, world!'})

手動でコンテキストを作成する

シンプルなテンプレートの場合、手動でコンテキストを作成することもできます。

# テンプレートコンテキストを作成
context = {
  'my_variable': 'Hello, world!',
  # 必要なその他のコンテキスト変数
}

# テンプレートを取得
template = loader.get_template('mytemplate.html')

# テンプレートをレンダリング
rendered_html = template.render(context)
  • 新しいコードを書く場合は、上記の代替方法を使用することをお勧めします。
  • 将来的に削除される可能性があります。
  • RequestContextはDjango 2.0以降で非推奨になっています。