Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda