简体   繁体   English

get_context_data 方法在 django 中不起作用

[英]get_context_data method doesn't work in django

I have a class based view that inherits from CreateView and I have some fields that I want to be sent in the page through context dictionary and the get_context_data doesn't work because non of the context fields doesn't appear in the page.我有一个从 CreateView 继承的基于 class 的视图,并且我有一些字段要通过上下文字典在页面中发送,而 get_context_data 不起作用,因为没有上下文字段不会出现在页面中。

the class: class:

class IncomeCreateListView(CreateView):
    template_name = 'users/incomes_&_spendings.html'
    form_class = IncomeCreateForm

    def get_context_date(self, **kwargs):
        context = super().get_context_data(self, **kwargs)
        incomes = Incomes.objects.filter(
            user=self.request.user, created_date__year=datetime.now().year, created_date__month=datetime.now().month)
        if datetime.now().month == 1:
            incomes_last_month = Incomes.objects.filter(
                user=self.request.user, created_date__year=datetime.now().year - 1, created_date__month=datetime.now().month + 11)
        else:
            incomes_last_month = Incomes.objects.filter(
                user=self.request.user, created_date__year=datetime.now().year, created_date__month=datetime.now().month - 1)
        total_incomes = round(assembly(incomes), 2)
        total_incomes_last_month = round(
            assembly(incomes_last_month), 2)
        context['title'] = 'Incomes'
        context['object_name'] = 'Incomes'
        context['primary_color'] = 'primary'
        context['objects'] = incomes
        context['total_sum'] = total_incomes
        context['total_sum_last_month'] = total_incomes_last_month
        context['year'] = datetime.now().year,
        context['month'] = datetime.now().month,
        return context

    def form_valid(self, form):
        return super().form_valid(form)

You wrote get_context_datE , while its get_context_datA你写了get_context_datE ,而它的get_context_datA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM