2017-06-28 15 views
1

DBの代わりに外部APIから受け取った大きなデータリストを表示するテンプレートがあります。Djangoでのページ設定TemplateView

私はこれが簡単にListViewであることを認識していますが、私はデータベースからデータを取得していないので、TemplateViewが最良の選択だと思われますが、データのリストを表示してページを設定するにはどうすればよいでしょうか?

現在、私は持っている:あなたはまだListViewコントロールを継承し、反復可能を返さなければならないget_queryset()メソッドをオーバーライドすることができます

....

 <div class="about"> 

      {% for quote in quotes %} 
      <h3>Supplier:{{ quote.supplierName }}</h3> 
       <div> 
       <p>Annual Cost: {{ quote.newSpend }}</p> 
       <p>Savings: {{ quote.newSavings }}</p> 
      </div> 
      <button class="btn btn-cta-primary">Proceed</button> 
      {% endfor %} 
     </div><!--//about--> 
+0

このデータはどのように取得していますか?それはあなたの意見であるように見えません – Emile

答えて

2

: ビュー

class QuotesResultsView(TemplateView): 
    template_name = 'site/quotes.html' 

    def get_context_data(self, **kwargs): 
     context = super().get_context_data(**kwargs) 
     context['quotes'] = self.request.session['quotes']['data'] 
     return context 

テンプレート。これは通常、

class QuotesResultsView(ListView): 
    template_name = 'site/quotes.html' 
    paginate_by = settings.QUOTES_PER_PAGE 
    context_object_name = 'quotes' 

    def get_queryset(self): 
     # Looks like your data is already an iterable 
     # if not convert it to iterable and return 
     return self.request.session['quotes']['data'] 

は次にテンプレートであなたがdocsから標準の改ページのスニペットを使用することができ、あなたの設定でQUOTES_PER_PAGEを設定し、あなたのviews.pyに

を、それをインポートすることを忘れてはいけないとページネーションを使用することができます。

+0

完璧なおかげで。 – Yunti

関連する問題