Error too many values to unpack

0

When I fill out the form, the data stores it correctly in the database, but the form does not work again and the error "too many values to unpack" appears. What can I do?

Here is my template:

<div class="col-md-20 animate-box">
            <h3></h3>
            <form method="post">{% csrf_token %} {{ form.as_p }}

                <div class="form-group">
                    <input type="submit" value="Enviar" class="btn btn-primary">

                </div>
            </form>

        </div>

My view:

def post(self, request):

        form =self.form_class(request.POST)

        if form.is_valid():
            form.save()
            messages.add_message(request, messages.INFO, 'La venta se adiciono correctamente ')

        else:
            messages.add_message(request, messages.ERROR, 'La venta no se pudo adicionar')

        return render(request, 'listar_contratos.html')
    
asked by tatiana hernandez 07.11.2018 в 20:43
source

1 answer

0

The problem is that you are not passing the context. Remember that to show the form in the template you have to pass the form in the context. The context is nothing more than a dictionary with the data:

def post(self, request):

    form =self.form_class(request.POST)

    if form.is_valid():
        form.save()
        messages.add_message(request, messages.INFO, 'La venta se adiciono correctamente ')

    else:
        messages.add_message(request, messages.ERROR, 'La venta no se pudo adicionar')

    context = {
        'form': form
    }

    return render(request, 'listar_contratos.html', context)

From what I see, you're using form_class , so I assume that your view is inheriting from FormView directly or indirectly. If all you want to do is show messages, there are special methods for when the form is valid or invalid:

def form_valid(self, form):
    # Cuando los datos en el formulario son válidos.
    messages.add_message(self.request, messages.INFO, 'La venta se adiciono correctamente ')
    return super().form_valid(form)


def form_invalid(self, form):
    # Cuando los datos en el formulario son inválidos.
    messages.add_message(self.request, messages.ERROR, 'La venta no se pudo adicionar')
    return super().form_invalid(form)

Notice that in the messages I am using self.request .

    
answered by 08.11.2018 в 15:34