Laxmi Kadariya
Laxmi Kadariya

Reputation: 1103

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

Upvotes: 1

Views: 5265

Answers (1)

JRajan
JRajan

Reputation: 702

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

Upvotes: 5

Related Questions