2016-08-17 17 views
6

私はデータ分析のためにパンダでdjangoを使用しようとしています。これについてのステップチュートリアルでは、単純なステップがないようです。私がオンラインで見たすべてのものは、あなたのdjangoのviews.pyファイルにコードを書く方法を説明していますが、最終製品をブラウザに表示する方法は示されていません。ここでdjangoテンプレートのdjango-pandasデータフレームを表示

は、私のviews.pyのコード

def index2(request): 
    qs = Product.objects.all() 
    df = read_frame(qs) 
    html= df.to_html 
    return HttpResponse(html) 

であるが、これは動作しません。詳細なヘルプは高く評価されます。私はいくつかのドキュメントを教えてください。実際、djangoのドキュメントのほとんどは単純な英語では書かれていません---それは私たちの中にはさらに混乱しています。ありがとうございました。ここで

+0

どういう意味ですか?期待どおりに動作しないか、まったく動作しません。データフレームの.to_html関数は、ブラウザに依存している裸のhtmlテーブルを提供するだけで、djangoテンプレートを使って適切なタグを追加する必要があります。 print(Product.objects.all()。count())は何を出力しますか? –

+0

私のブラウザには何も表示されず、テーブルはありません(何もありません)。私はprintステートメントを試して、出力があなたに感謝します。 –

答えて

7

は(https://github.com/wenzhixin/bootstrap-table)Django_Pandasを使用して、最小限でありながらエレガントなソリューションと「拡張ブートストラップテーブル」です

エレガンスは、JSONにパンダのデータフレームをエクスポートする機能から来て、ブートストラップ表のためのスクリプトは、それを消費しますJSONコンテンツ

HTMLテーブルは私たちのために書かれていますので、心配する必要はありません(下に、自分自身で行を書くことなく 'table'タグを含めるか、ループを参照してください)。インタラクティブそしてブートストラップはそれをかなり見えるようにします。

要件:ブートストラップ、jQueryの、Django_Pandas、wenzhixin /ブートストラップ・テーブル

models.py

from django.db import models 
from django_pandas.managers import DataFrameManager 

class Product(models.Model): 
    product_name=models.TextField() 
    objects = models.Manager() 
    pdobjects = DataFrameManager() # Pandas-Enabled Manager 

views.py

from models import Product 
def ProductView(request): 
    qs = Product.pdobjects.all() # Use the Pandas Manager 
    df = qs.to_dataframe() 
    template = 'product.html' 

    #Format the column headers for the Bootstrap table, they're just a list of field names, 
    #duplicated and turned into dicts like this: {'field': 'foo', 'title: 'foo'} 
    columns = [{'field': f, 'title': f} for f in Product._Meta.fields] 
    #Write the DataFrame to JSON (as easy as can be) 
    json = df.to_json(orient='records') # output just the records (no fieldnames) as a collection of tuples 
    #Proceed to create your context object containing the columns and the data 
    context = { 
      'data': json, 
      'columns': columns 
      } 
    #And render it! 
    return render(request, template, context) 

product.html

<script src='/path/to/bootstrap.js'> 
<script src='/path/to/jquery.js'> 
<script src='/path/to/bootstrap-table.js'> 
<script src='/path/to/pandas_bootstrap_table.js'> 
<table id='datatable'></table> 
<!-- Yep, all you need is a properly identified 
    but otherwise empty, table tag! --> 

pandas_bootstrap_ta ble.js

$(function() { 
    $('#datatable')({ 
    striped: true, 
    pagination: true, 
    showColumns: true, 
    showToggle: true, 
    showExport: true, 
    sortable: true, 
    paginationVAlign: 'both', 
    pageSize: 25, 
    pageList: [10, 25, 50, 100, 'ALL'], 
    columns: {{ columns|safe }}, // here is where we use the column content from our Django View 
    data: {{ data|safe }}, // here is where we use the data content from our Django View. we escape the content with the safe tag so the raw JSON isn't shown. 
    }); 
}); 
+2

私はあなたの答えを編集していませんが、私が慣れていない機能を使用しているかもしれませんが、あなたのjの構文は次のようにしてください:$( '#datatable.pandas')。bootstrapTable({あなたが使用している機能がブートストラップにあるとは思わない。同じ構文を持つように見えるbootstraptablesという別個のアドオンがあるか? –

+1

KBあなたは少なくとも、以前言及しなかったサードパーティのツールについては正しい:https://github.com/wenzhixin/bootstrap-table。それに応じて更新されました。 –

+0

KBここでは簡単にするために、テーブルクラスを削除してIDにハードコードするだけです:$( '#datatable') –

関連する問題