2014-06-11 73 views
0

我目前正試圖從django cms插件中導出一個csv文件。唯一的問題是,我不知道如何從渲染函數中返回HttpResponse。在Django中導出CSV

任何和所有的幫助,非常感謝!

cms_plugins.py

from cms.plugin_base import CMSPluginBase 
from cms.plugin_pool import plugin_pool 
from data_viewer.models.data_view import DataPlugin 
from django.utils.translation import ugettext as _ 
from django.db.models import Q 
from django.core.paginator import Paginator 
import base64 
from django.http import HttpResponse 
import csv 

class CMSDataPlugin(CMSPluginBase): 
    model = DataPlugin 
    name = _("Data Viewer") 
    render_template = "data_viewer/default.html" 

    def makeQstring(self, qlist, q): 
     out = [] 
     for fd in qlist: 
      out.append("Q(%s__icontains='%s')" % (fd, q)) 

     return ' | '.join(out) 

    def render(self, context, instance, placeholder): 
     is_csv = context['request'].GET.get('csv', False) 

     if is_csv == 'true': 
      response = HttpResponse(content_type='text/csv') 
      response['Content-Disposition'] = 'attachment; filename="somefilename.csv"' 

      writer = csv.writer(response) 
      writer.writerow(['First row', 'Foo', 'Bar', 'Baz']) 
      writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"]) 

      context = response !!!!! I need to return this response, but render can only return a context object 

     else:  
      gridData = dict() 
      qList = False 
      G = context['request'].GET 
      q = G.get('find', False) 
      paNum = G.get('p',1) 
      if q == False: 
       q = G.get('q', False) 
      if q and instance.filter_fields: 
       qList = instance.filter_fields.strip().split(',') 

      ordBy = '' 
      if G.get('o', False): 
       ordBy = ".order_by('%s')" % G.get('o') 

      if instance and instance.layout: 
       self.render_template = "data_viewer/"+instance.layout 
       exec("from data_viewer.models.%s import %s as GridData" % (instance.source, instance.name)) 

       if GridData and instance.init_filter: 
        if instance.init_filter.strip().startswith('Q') and qList: 
         instance.filter += ' & (%s)' % self.makeQstring(qList, q) 
        gridData = eval("GridData.objects.filter(%s)%s" % (instance.init_filter, ordBy)) 
       elif GridData and qList: 
        gridData = eval("GridData.objects.filter(%s)%s" % (self.makeQstring(qList, q), ordBy)) 
       elif GridData: 
        gridData = eval("GridData.objects.all()%s" % ordBy)   

       pg = Paginator(gridData, instance.page_count) 
      conObj ={ 
       'data':pg.page(paNum), 
       'pagination':pg, 
       'search':{ 
        'q':q, 
        'p':paNum, 
        'o':G.get('o',False), 
       }, 
       'object':instance, 
       'placeholder':placeholder 
      } 
      print instance.name 
      if instance.name == 'PublicContactCards': 
       print 'adding contacts' 
       import vobject 
       conObj['cards'] = [] 
       for d in conObj['data']: 
        vcard = vobject.readOne(d.carddata) 
        conObj['cards'].append(vcard) 

      context.update(conObj) 
     return context 

plugin_pool.register_plugin(CMSDataPlugin) 
+1

這是什麼插件?如果是django-cms或類似的東西,你應該這樣說。 –

+0

是的,它是django cms。我現在已經添加了這個問題。 – Greg

+1

我沒有與django_cms一起工作,但我已經完成了csv導出。如果需求是返回一個上下文對象,我的猜測是csv響應需要在其他地方或者插件的其他鉤子中生成。 –

回答