2011-07-04 8 views
3

どうやってプログラムでDjangoテンプレートのセクションを変更することができますか?Django Template:テンプレートのセクションを並べ替えることができますか?

 
{% for it in itemlist_1 %} 
{{it.unique_display_function_1}} 
{%endfor%} 

{% for it in itemlist_2 %} 
{{it.unique_display_function_2}} 
{{it.unique_display_function_2a}} 
{{it.unique_display_function_2b}} 
{%endfor%} 

... 

{% for it in itemlist_n %} 
{{it.unique_display_function_n}} 
{{it.unique_display_function_n_sub_x}} 
{{it.unique_display_function_n_sub_xyz}} 
{%endfor%} 


このテンプレートは、外部設定をレンダリングされるたびにitemlistsがテンプレートでレンダリングされている注文内容を決定するように、私は一般的なDjangoのテンプレートを構築することができる方法:

は、このテンプレートを考えると?

したがって、n個のセクションのリストは、いくつかの外部設定に従って任意の順序で表示されます。

注:テンプレートの各セクションには多くのサブパーツがあり、実際はかなり長いことを示すために更新されています。

+0

あなたの商品リストを一覧に並べて表示することはできません。 –

答えて

2

私はのリストを作成することをお勧めしたいですユーザーが指定する順序に対応するビュー内のセクション名。

def view(request): 
    # this list can also be built dynamically based on user preferences 
    item_list = ["section_one.html", "section_two.html", "section_three.html"] 
    return render_to_response('main_template.html', RequestContext(request, locals())) 

次にテンプレートであなたがサブテンプレートは、「名.html」形式と命名されている以下のようなサブテンプレートとして、各セクションにレンダリングすることができます:ここで

{%for item in item_list%} 
    {% include item %} 

ことのための参照ですインクルードタグ:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#include

1

ビュー内のセクションの順序を変更することが容易になるだろう:

ビュー:

def view(request): 
    context = {} 
    context['items'] = [] 

    #decide the order and put the items into the context 

    return render_to_response('template.html',context,context_instance=RequestContext(request)) 

テンプレート:

{%for itemlist in items%} 
    {%for item in itemlist%} 
     {{item.display_function}} 
    {%endfor%} 
{%endfor%} 
関連する問題