2016-08-17 18 views
6

Veri analizi için django'yu pandalarla kullanmaya çalışıyorum. Bu konuda adım adım basit bir adım yok gibi görünüyor. Çevrimiçi olarak gördüğüm tüm kullanıcılar, django views.py dosyanıza kodu nasıl yazacağınızı açıklar, ancak hiçbiri tarayıcıda son ürünün nasıl görüntüleneceğini gösterir. İşte django-pandas veri çerçevesini django şablonunda görüntüleme

benim views.py

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

kod ama bu işe yaramaz. Herhangi bir ayrıntılı yardım takdir edilecektir. Lütfen bana sadece bazı belgelere işaret etmeyin. Aslında, django'nun belgelerinin çoğu basit bir ingilizce ile yazılmamaktadır - bazılarımız için daha da kafa karıştırıcıdır. Teşekkür ederim. İşte

+0

Ne demek demek çalışmıyor? Beklendiği gibi çalışmıyor ya da tam olarak çalışmıyor mu? dataframe'in .to_html işlevi sadece bir web sayfası değil, bir web sayfası olan çıplak bir html tablosu verir, bir django şablonu kullanarak etrafınıza uygun etiketler eklemeniz gerekebilir. Baskı (Product.objects.all(). Count()) çıkışı nedir? –

+0

Tarayıcıda hiçbir şey gösterilmiyor, tablo yok (hiçbir şey). Baskı beyanını deneyeceğim ve çıktıyı görebiliyorum. –

cevap

7

Django_Pandas kullanarak minimal ama zarif bir çözüm ve bir 'genişletilmiş Bootstrap tablo' olduğunu (https://github.com/wenzhixin/bootstrap-table)

şıklık JSON bir Pandalar DataFrame ihracat yeteneği gelir ve bu tüketmeye Bootstrap Masa komut dosyası için JSON içeriği.

HTML tablosu bizim için yazılmıştır, bunun için endişelenmenize gerek yok (sadece 'tablo' etiketini satır satırlarını yazmadan ve hatta döngü için dahil olmak üzere aşağıya bakın.) İnteraktif. Ve Bootstrap güzel görünüyor.

şartlar: Önyükleme, JQuery, Django_Pandas, wenzhixin/önyükleme tablo

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

Tanımadığınız bir işlevi kullandığınız için cevabınızı düzenlemedim, ancak js'nizdeki sözdiziminizin şöyle olması gerektiğini düşünüyorum: $ ('# datatable.pandas'). BootstrapTable ({. Kullanmakta olduğunuz işlevselliğin bootstrap olduğunu sanmıyorum.Aynı sözdizimine sahip görünen bootstraptables adı verilen ayrı bir eklenti var mı? –

+1

KB Önceden bahsetmeyi ihmal ettiğim üçüncü taraf aracı hakkında en azından siz haklısınız. : https://github.com/wenzhixin/bootstrap-table Uygun şekilde güncellendi –

+0

KB Ben burada basitlik için tablo sınıfını kaldırabilir ve sadece ID gibi bir kod yazabilirim: $ ('# datatable') ({ –

İlgili konular