2017-03-08 156 views
0

我有一个类连接到一个模板,里面有两个函数,一个是认证用户(使用我的数据库中可用的凭证),第二个是实际获取一些数据并将其推送到我的模板。但是使用print('whatever')我看到,当我调用该类时,这两个函数都不会被调用。为什么?在基于类的视图中调用两个函数 - Django

views.py

class GenerateReport(TemplateView): # This view is responsable to access users data and return it 
    template_name = 'ga_app/report.html' 
    def generate_report(request): # authenticate user 
     c = CredentialsModel.objects.get(user_id = request.user.id) 
     credentials = c.credentials 
     http = httplib2.Http() 
     http = credentials.authorize(http) 
     service = build('analyticsreporting', 'v4', http=http) 
     print('This first function is not called') 

    def print_data(request): # Get some data 
     profile_id = GoogleProperty.objects.get(user_id = request.user.id) 
     some_data = service.data().ga().get(
      ids='ga:' + profile_id, 
      start_date='7daysAgo', 
      end_date='today', 
      metrics='ga:sessions').execute() 
     print('This second function neither') 
     return render(request, self.report, {'some_data': some_data}, {'profile_id': profile_id}) 

urls.py

url(r'^re/$', GenerateReport.as_view(), name='re'), 

壳显然并没有显示任何东西打印出来,和模板不会呈现metrics和/或profile_id

回答

2

这些功能将不会被默认调用,所以你需要自己调用它们。查看TemplateView的文档,看起来您需要实现get_context_data方法,您可以在其中调用这些函数,并返回模板的上下文。

相关问题