【问题标题】:How to use get_context_data in django如何在 django 中使用 get_context_data
【发布时间】:2013-11-19 10:42:08
【问题描述】:

我有 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'

短信记录器

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 应用程序中获取数据。如何通过 TemplateView 子类实现它

【问题讨论】:

  • smslogger 必须是模型而不是应用程序
  • 应用没有数据。模型可以。
  • 你能和我们分享一下smslogger模型吗?这将有助于找出问题。

标签: python django


【解决方案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

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多