简体   繁体   中英

How to use get_context_data in django

I have 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

{% 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. How can I acheive it through TemplateView subclass

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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