2011-07-04 3 views
3

어떻게 프로그래밍 방식으로 장고 템플릿의 섹션을 변경할 수 있습니까?Django 템플릿 : 템플릿의 섹션을 재정렬 할 수있게 만드시겠습니까?

 
{% 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이 템플릿에 렌더링되는 순서를 파악한 외부 렌더링 할 때마다 있도록 일반 장고 템플릿을 만들 수 :

이 템플릿을 감안할 때?

따라서 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())) 

그런 다음 템플릿은 서브 템플릿은 "NAME.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%} 
관련 문제