如何在Django中使用get_context_data [英] How to use get_context_data in django

查看:63
本文介绍了如何在Django中使用get_context_data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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应用程序获取数据.我该如何通过TemplateView子类来实现

I want to have the data from smslogger app. How can I acheive it through TemplateView subclass

推荐答案

您能否让我们知道smslogger.py包含哪些内容?

Could you let us know what smslogger.py contains ?

我认为您可能需要做这样的事情.

I think you might be need to do something like this.

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

这篇关于如何在Django中使用get_context_data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆