简体   繁体   中英

Pass an id from a template to a view in django

I´m having some problems with this. I have a form like this:

<form action="/" method="post">
        {% csrf_token %}
        <input type="button" name="_mail" value="Enviar Mail" id="buttonId"> 
</form>

it´s just a buttom. Then in my views i have this code:

def verFactura(request, id_factura):
    fact = Factura.objects.get(pk = id_factura)
    cliente = Cliente.objects.get(factura = fact)
    template = 'verfacturas.html'
    iva = fact.importe_sin_iva * 0.21
    total = fact.importe_sin_iva + iva

    extra_context = dict()
    extra_context['fact'] = fact
    extra_context['cliente'] = cliente
    extra_context['iva'] = iva
    extra_context['total'] = total


    if "_mail" in request.POST: 
        send_templated_mail(template_name='receipt',
                    from_email='imiguel@exisoft.com.ar',
                    recipient_list =['ignacio.miguel.a@gmail.com'],
                    context=extra_context)
        return HttpResponseRedirect('../facturas')
    else:(if i don´t write this else i get an error)
        return render(request,template, extra_context)

return render(request,template, extra_context)

And a member of SO suggested to use javascript to handle the click event like this:

<script type="text/javascript">
    $(document).ready(function(){
        $('#buttonId').click(function(){
        alert("Email enviado");
  });
});

i know nothing about javascript but i was hoping that with only this i could call the IF statement in the view. But obviously this part of the code never gets call and i don´t recieve any email.

So, can anyone tell me where is my mistake? i have a feeling that the part of the If statement is wrong but i don´t why

I would really appreciate the help. Thank you

if "buttonId" in request.POST.get('click', False):

There's no "click" parameter. With this instruction you try to iterate ("in") through the False value.

Try:

if '_mail' in request.POST:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM