2017-03-08 115 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方法,您可以在其中調用這些函數,並返回模板的上下文。

相關問題