如何在django中使用get_context_data

时间:2013-11-19 10:42:08

标签: python django

我有 的 Views.py

class newChartView(TemplateView):
    template_name = "new_report_view.html"

    def get_context_data(self, **kwargs):
        context = super(newChartView, self).get_context_data(**kwargs)
        context['count'] = smslogger.objects.all()
        return context

new_report_view.html

{% for x in count %}
{{ x.count }}
{% endfor %}

并显示错误

'module' object has no attribute 'objects'

smsloggger

model

class Log(models.Model):
   date= models.DateField()
   count=models.CharField(max_length=100)
   class Meta:
        verbose_name_plural = "SMS Log"

   def __unicode__(self):
        return self.date,self.count

我想从smslogger app获取数据。我如何通过TemplateView子类

实现它

1 个答案:

答案 0 :(得分:1)

你能告诉我们smslogger.py包含的内容吗?

我想你可能需要这样做。

from smslogger import YourModel

class newChartView(TemplateView):
    template_name = "new_report_view.html"

    def get_context_data(self, **kwargs):
        context = super(newChartView, self).get_context_data(**kwargs)
        context['count'] = YourModel.objects.all()
        return context