テンプレートを再利用する場合は、汎用レイアウトの基本テンプレートを作成し、個々のページに詳細なテンプレートを使用するだけです。これは既にDjango documentationによって詳細にカバーされています。
私はそれらの一般的な部分(例えば、使用がオンになっているサイトの現在の一部をハイライトメニューを言う)のために行う傾向にある何、次のように似て、私自身のrender_to_response
関数を作成することです:
from django.shortcuts import render_to_response as django_render_to_response
def render_to_response(template, params, context_instance):
return django_render_to_response(template,
AppendStandardParams(request, params),
context_instance)
ApplyStandardParams
方法は、現在のパスに基づいてメニューを構成:
def AppendStandardParams(request, params):
if request.META['PATH_INFO'].startswith('/customer'):
params['category'] = 'customer'
params['title'] = 'Customer overview'
# and so on for all different parts
この例では、これらcategory
とtitle
タグは、メニューを強調するために使用されるいくつかの値であり、設定タイトルなどが含まれます。たとえば:
<!-- Customer menu entry: change class if this is the current category. -->
<li{% if category == "customer" %} class="selected"{% endif %}>Customers</li>
は最後に、代わりに通常のrender_to_response
輸入の、ビューでそれを使用するために、私はちょうどビューで私のカスタムバージョンが利用できるようになりfrom lib.views import *
ような何かを、行います。この方法では、ビュー内のすべてのコードの構文は同じですが、新しいビューまたはアプリケーションを作成するたびにメニューをカスタマイズする必要はありません。
出典
2011-10-26 06:43:24
jro
それはトリックを行う必要があります:)タンクあなた –