今回質問させて頂きたいのは、
Djangoにおけるpandas DataFrameデータの表示についてです。
view.pyでDaraFrameを作成し、そこにBookモデルのデータを追加しています。
そのDaraFrame(df)をテンプレート(index.html)上で表示したいのですが、
以下のエラーが出てうまく行きません。
ご教授のほどよろしくお願いします。
error
1TemplateSyntaxError at /index/ 2Could not parse the remainder: '()' from 'df.iteritems()'
models
1class Book(models.Model): 2 name = models.CharField('書籍名', max_length=255) 3 price = models.IntegerField('価格', blank=True, default=0) 4 publisher = models.CharField('出版社', max_length=255, blank=True)
views
1class index(generic.ListView): 2 model = Book 3 context_object_name = 'books' 4 template_name = 'index.html' 5 6 def get_context_data(self, **kwargs): 7 context = super().get_context_data(**kwargs) 8 columns = ['name', 'price', 'publisher'] 9 df = pd.DataFrame(columns=columns) 10 11 for i in Book.objects.all(): 12 se = pd.Series([ 13 i.name, 14 i.price, 15 i.publisher 16 ], columns) 17 # 1行ずつDataFrameに追加 18 df = df.append(se, ignore_index=True) 19 context['df'] = df 20 return context
indexhtml
1 <div class="table-sm"> 2 <table class="table table-bordered table-hover"> 3 <thead> 4 <tr class="table-secondary"> 5 <th>名前</th> 6 <th>価格</th> 7 <th>出版社</th> 8 </tr> 9 </thead> 10 <tbody> 11 {% for row in df %} 12 <tr> 13 <td>{{ row['name'] }}</td> 14 <td>{{ row['price'] }}</td> 15 <td>{{ row['publisher'] }}</td> 16 </tr> 17 {% endfor %} 18 </tbody> 19 </table> 20 {% csrf_token %} 21 </div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。