Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Update context of FormView only in certain condition

I have a code in my FormView in Django:

class SearchView(LoginRequiredMixin, FormView):
    template_name = "dic_records/search_form.html"
    form_class = KokyakuForm
    success_url = 'search'
    fetched_data = []

    def form_valid(self, form):
        print(form.cleaned_data)
        kokyaku_instance = FetchKokyaku(form.cleaned_data['name_search'])
        err_check = kokyaku_instance.connect()
        kokyaku_instance.get_data()
        if err_check is False:
            messages.error(self.request, "データを出力できませんでした")
            return super(SearchView, self).form_valid(form)
        
        kokyaku_list = list(kokyaku_instance.get_data())
        if len(kokyaku_list) == 0:
            messages.error(self.request, "検索のデータを見つけられませんでした")
            return super(SearchView, self).form_valid(form)
        self.fetched_data = kokyaku_list
        return super(SearchView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        """Use this to add extra context."""
        context = super(SearchView, self).get_context_data(**kwargs)
        context['list'] = self.fetched_data
        return context

And I would like to provide to template a list of values kokyaku_list from context variable, but only if my form is valid and data are fetched from kokyaku_instance.get_data().

The code above wont pass values

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!