2016-11-29 14 views
0

私はDjangoプロジェクトを持っており、そのページの1つでは、データベースの情報の一部に関するレポートを表示しています。Django-要素をあるビューから別のビューにコピーする方法は?

レポートを表示する要素には、データベース内のさまざまな要素に関する「PDF形式の」レポートを表示する多数のタブがあります。

タブの1つに表示されている情報を別のタブに追加したいのですが、どうすればいいのか分かりません。

私は、他のタブにコピーする情報を持っているタブの表示がで定義されます。

def report_ccis(request, project_id): 
    """ CCI items styled for pdf """ 
    project = Project.objects.get(id=project_id) 
    budget = get_current_budget(project_id) 

    cci_total_exc = budget.cci_total_exc_vat_final 
    cci_grouped_items = budget.cci_items.all().order_by('project_room', 'name') 

    context = { 
     'project': project, 
     'cci_total_exc': cci_total_exc, 
     'cci_grouped_items': cci_grouped_items, 
     'webview': 1, 
    } 

    try: context['current_budget'] = project.budget_versions.get(current_marker=1) #For option name/date on top of pdfs 
    except ObjectDoesNotExist: pass 

    if request.GET.get('stage') == 'pd': 
     """ Render post deposit homepage """ 
     context['html'] = render_to_string('costing/report2_ccis.html', context) 
     context['active_tab'] = '4' 
     return render(request, 'costing/reports_post_deposit.html', context) 
    else: 
     """ Render pre deposit homepage """ 
     context['html'] = render_to_string('costing/report_ccis.html', context) 
     context['active_tab'] = '5' 
     return render(request, 'costing/reports_pre_deposit.html', context) 

と私は、この情報を表示したいタブのビューを使用して定義されています。私はそれに最初viewからコードを付加することにより、第2 veiw第1 viewに表示される情報を追加しようとした

def report_overview(request, project_id): 
    """ Budget overview styled for pdf """ 
    project = Project.objects.get(id=project_id) 

    budget = get_current_budget(project_id) 
    if not budget and not project.budget_versions.filter(current_marker=1): 
     Budget.objects.create(project=project, current_marker=1) 

    cci_total_exc = budget.cci_total_exc_vat 
    item_total_exc = budget.item_total_exc_vat() 
    total_exc = cci_total_exc + item_total_exc 
    total_exc_2 = budget.grand_total_exc_vat 
    total_inc = budget.grand_total_inc_vat 
    #----- Add CCIs to the 'Overview tab' ----- 
    cci_total_exc_final = budget.cci_total_exc_vat_final # ERF(29/11/2016 @ 1615) Changed from cci_total_exc to cci_total_exc_final 
    cci_grouped_items = budget.cci_items.all().order_by('project_room', 'name') 
    #-----(29/11/2016 @ 1615) ----- 

    context = { 
     'project': project, 
     'budget': budget, 
     'cci_total_exc': cci_total_exc, 
     'item_total_exc': item_total_exc, 
     'total_exc': total_exc, 
     'total_exc_2': total_exc_2, 
     'total_inc': total_inc, 
     #-----(29/11/2016 @ 1615) Add CCIs to the 'Overview tab'----- 
     'cci_grouped_items': cci_grouped_items, 
     'webview': 1, 
     #-----(29/11/2016 @ 1615) ----- 
    } 
    print '****************************************************' 
    try: 
     context['current_budget'] = project.budget_versions.get(current_marker=1) #For option name/date on top of pdfs/pages 
     print 'OPTION NAME', project.budget_versions.get(current_marker=1) 
    except ObjectDoesNotExist: 
     print 'No option found with current marker' 
     pass 

    if request.GET.get('pdf'): 
     template = get_template('costing/report_overview.html') 
     html = template.render(context) 

     file = open('test.pdf', "w+b") 
     pisaStatus = pisa.CreatePDF(html.encode('utf-8'), link_callback=fetch_resources, dest=file, 
      encoding='utf-8') 

     file.seek(0) 
     pdf = file.read() 
     file.close()    
     return HttpResponse(pdf, 'application/pdf') 

    else: 
     context['webview'] = 1 
     context['html'] = render_to_string('costing/report_overview.html', context) 
     context['active_tab'] = '1' 
     return render(request, 'costing/reports_pre_deposit.html', context) 

、すなわち追加:

を私は、ブラウザでページを表示し、レポートの[概要]タブを選択すると report_overviewビューの終わりまで
if request.GET.get('pdf'): 
    """ Render post deposit homepage """ 
    context['html'] = render_to_string('costing/report2_ccis.html', context) 
    context['active_tab'] = '1' 
    return render(request, 'costing/reports_post_deposit.html', context) 
else: 
    """ Render pre deposit homepage """ 
    context['html'] = render_to_string('costing/report_ccis.html', context) 
    context['active_tab'] = '1' 
    return render(request, 'costing/reports_pre_deposit.html', context) 

、しかし、私はまだちょうど情報のどれビフォア表示されていたものを見ること私はreport_ccisからコピーしましたが表示されます。

これはなぜですか? viewによって表示された情報を他のものにコピーするにはどうすればよいですか?

編集

report_overview(...)ビューは、同じテンプレートを返すの両方report_ccis(...) &ので、そのファイルのHTMLは次のとおりです。

{% extends "costing/reports_tabbed.html" %} 
{% load staticfiles utilities %} 

{% block title2 %} 
    | Pre-deposit reports 
{% endblock title2 %} 

{% block page_title %} 
    <a id="topbar-shortcuts" data-view-url="{% url 'hub:open_sidebar' %}?app={{app.name}}&p={{project.id}}&po=1"> 
     <span class="m-l-md">Reports</span> <img class="icon open text-sm m-l-md" src="{% static 'img/down-lt.png' %}" > 
    </a> 
    <div id="topbar-results" class="{{app.color}}" style="display:none;"></div> 
{% endblock page_title %} 


{% block tabs %} 
    {% with 'Overview, Construction budget, Schedule of works, Client choice items'|listify as tabs %} 
     {% for tab_name in tabs %} 
      {% with forloop.counter as tab %} 
       {% if not tab == active_tab|add:0 %}<a class="tab" href="{% url 'costing:report_tabbed' project.id %}?tab={{tab}}">{% else %}<a class="active tab">{% endif %}{{tab_name}}</a> 
      {% endwith %} 
     {% endfor %} 
    {% endwith %} 
{% endblock tabs %} 

このHTMLは 'tabbed-ビュー' のページ要素を表示しています私がしたいのは、report-ccis(...)ビューで表示されたタブの内容をreport-overview(...) view, also keeping all of the information currently displayed by theレポートの概要(...)で表示されたタブにコピーすることです。

{% extends "pdf_base.html" %} 
{% load money_handling %} 

{% block content_overview %} 
{% endblock content_overview %} 
{% block content_construction %} 
{% endblock content_construction %} 
{% block content_schedule_of_works %} 
{% endblock content_schedule_of_works %} 
{% block content_report_by_class %} 
{% endblock content_report_by_class %} 
{% block content_ccis %} 
    {{block.super}} 
{% endblock content_ccis %} 

編集

ああ、実際に、私は両方とも同じテンプレートを返すviewsものの、次のコードを持っている私のプロジェクト構造でreport_ccis.htmlファイルがあることを発見しました

context['html'] = render_to_string('costing/report_ccis.html', context) 

report_overview(...)ビューでifelse文はラインを持っています

このタブが選択されていると、HTMLファイルのこの 'タブ付き'ビュー部分が変更されている場所/方法は?

このコンテンツをHTMLのブロックのreport-overviewタブに追加するにはどうすればよいですか?:それはと思われ

def report_tabbed(request, project_id): 
    project = Project.objects.get(id=project_id) 
    tab = request.GET.get('tab', '1') 

    tab_map = { 
     '1': reverse('costing:report_overview', args=[project.id]), 
     '2': reverse('costing:report_construction', args=[project.id]), 
     '3': reverse('costing:budget', args=[project.budget_overview.version.pk])+"?report=1", 
    # '4': reverse('costing:report_by_class', args=[project.id]), 
     '4': reverse('costing:report_ccis', args=[project.pk]), 
    } 
    return HttpResponseRedirect(tab_map[tab]) 

{% block tabs %} 
    {% with 'Overview, Construction budget, Schedule of works, Client choice items'|listify as tabs %} 
     {% for tab_name in tabs %} 
      {% with forloop.counter as tab %} 
       {% if not tab == active_tab|add:0 %}<a class="tab" href="{% url 'costing:report_tabbed' project.id %}?tab={{tab}}">{% else %}<a class="active tab">{% endif %}{{tab_name}}</a> 
      {% endwith %} 
     {% endfor %} 
    {% endwith %} 
{% endblock tabs %} 

編集

私はちょうどHTMLページ上の「タブ付きの」レポート領域のためのビューがあることを発見しました、で定義されていますこのviewは、ページの「タブ付き」領域に表示されるレポートを切り替えるために使用されます。 '4':reverse('costing:report_ccis', args=[project.pk]),'1':reverse('costing:report_overview', args=[project.id]),にマージする方法はありますか?

+0

あなたはテンプレートを変更する必要があると思います。また、ビューに何かが表示されないことについての質問を投稿すると、関連するテンプレートコードが回答に役立つことがあります。 – Ivan

+0

両方のビューで参照されているテンプレートのテンプレートコードを追加しました。 – someone2088

答えて

1

あなたが記述している問題 - 重複コード - は、リファクタリングによって対処される多くの一般的な問題の1つです。ここで適用するリファクタリングアクションは、Extract Methodと呼ばれます。

共有コードの場合、共通コードを別の関数に抽出し、その関数を必要な場所から呼び出します。

共有テンプレートのコンテンツでは、共通のコンテンツを別のテンプレートファイルに、{% include … %}そのテンプレートを必要なすべての場所から抽出します。

+0

「共通の」コードは現時点では実際には共通ではありません。別々のビュー(異なるコード)があり、それぞれが同じHTMLテンプレート上に「タブ付き」表示領域を表示します。どちらのビューも同じテンプレート(同じHTMLファイル)を返しますが、「タブ付き」表示エリアの別のタブが各ビューで表示されます。最初のビューのブラウザに表示されるURLは、 'https:// www.mysite.co.uk/costing/6172/report/ccis /'、 – someone2088

+0

であり、2番目のビューのブラウザに表示されるURLは ' https:// www.mysite.co.uk/costing/6172/report/overview/'。しかし、ブラウザの「ビュー」を切り替えるには、ページの「タブ付きコンテンツ」領域の各タブを選択します。つまり、ページ全体が再読み込み/更新されず、「タブ付きコンテンツ」領域のみが変更されます。別のタブを選択するとブラウザのURLが変更されます。 – someone2088

関連する問題