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