派生基于django类的视图和上下文数据

时间:2013-04-21 22:11:47

标签: django django-class-based-views

我有问题。我至少有三个一级和一个上下文数据滴定谁是页面的标题。

表单的一个通用类:

class Myformview(generic.FormView):
    form_class = None
    template_name = None
    titre=None

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        """on a besoin pour le method decorator"""
        return super(Myformview, self).dispatch(*args, **kwargs)

    def get_context_data(self, *args, **kwargs):
        context= super(Myformview, self).get_context_data(*args, **kwargs)
        if self.titre is not None:
            context.update({'titre':self.titre})
        return context

因为我有不同的方法来导出基类来导出数据:

class ExportViewBase(FormView):
    template_name = 'param_export.djhtm'  # nom du template
    model_initial = None  # model d'ou on tire les dates initiales
    extension_file = None
    nomfich = None
    debug = False
    form_class = None

    def export(self, query):  
        """
        fonction principale mais abstraite
        """
        raise django_exceptions.ImproperlyConfigured(
            "attention, il doit y avoir une methode qui extrait effectivement")

    def get_initial(self):
        """gestion des donnees initiales"""
        if self.model_initial is None:
            raise django_exceptions.ImproperlyConfigured("un modele d'ou on tire les dates initiales doit etre defini")
        date_min = self.model_initial.objects.aggregate(
            element=models_agg.Min('date'))['element']
        date_max = self.model_initial.objects.aggregate(
            element=models_agg.Max('date'))['element']
        return {'date_min': date_min, 'date_max': date_max}

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        """on a besoin pour le method decorator"""
        return super(ExportViewBase, self).dispatch(*args, **kwargs)

    def form_valid(self, form):
        """si le form est valid"""
        reponse = self.export(query=form.query)  
        if self.nomfich is None:
            raise django_exceptions.ImproperlyConfigured('nomfich undefined')
        if self.extension_file is None:
            raise django_exceptions.ImproperlyConfigured('extension_file undefined')
        reponse["Cache-Control"] = "no-cache, must-revalidate"
        reponse['Pragma'] = "public"
        reponse["Content-Disposition"] = "attachment; filename=%s_%s.%s" % (self.nomfich, time.strftime("%d_%m_%Y-%H_%M_%S",time.localtime()), self.extension_file)
        return reponse

和要导出的有效类:

class Export_view_sql(export_base.ExportViewBase):
    extension_file = "sql"
    debug = True
    nomfich = "export_full"
    model_initial=models.Ope
    form_class = export_base.Exportform_ope
    titre="export sql"
    def export(self, query):
        pass#code to export

但是,它没有按预期更新滴度。这是什么原因?

如果我在ExportViewBase中设置滴度,它可以正常工作,但在Export_view_sql中则不然。

0 个答案:

没有答案