简体   繁体   English

如何在Django中使用get_context_data

[英]How to use get_context_data in django

I have Views.py 我有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

and new_report_view.html as new_report_view.html作为

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

and it show error 它显示错误

'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

I want to have the data from smslogger app. 我想从smslogger应用程序获取数据。 How can I acheive it through TemplateView subclass 如何通过TemplateView子类实现

Could you let us know what smslogger.py contains ? 您能告诉我们smslogger.py包含什么吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM