Django:将变量从post传递到get_context_data()

时间:2019-01-15 07:41:19

标签: django django-models django-forms django-templates

我想将值从邮政表格传递到get_context_data函数。但是问题是,当我在post函数中使用该值时,它会打印正确的值,例如:self.start_date,但我在get_context_data中打印的却是None。当我删除此行start_date = None时,它给出了错误'JobListView' object has no attribute 'start_date'

class JobListView(LoginRequiredMixin, generic.TemplateView):
    template_name = 'admin/jobs/job.html'

    # if i remove this then i get error 'JobListView' object has no attribute 'start_date'
    start_date = None

    def get_context_data(self, **kwargs):   
        context = super(JobListView, self).get_context_data(**kwargs)
        company_name = self.request.user.userprofile.user_company
        context['jobs'] = Jobs.objects.exclude(job_is_deleted = True)
        context['form'] = JobSearchForm()

        print(self.start_date) # this print as None

        return context

    def post(self, request, *args, **kwargs):
        self.start_date = self.request.POST.get('start_date',None)

        print(self.start_date) # this print correct value from form

        return HttpResponseRedirect('/useradmin/job/')

0 个答案:

没有答案