我觉得标题很清楚。我想知道用户何时单击按钮在my views.py中的函数中运行一段代码。假设我有这个html:
<div>
<input type="button" name="_mail" value="Enviar Mail">
</div>
如果用户单击此代码,我将运行此代码:
send_templated_mail(template_name='receipt',
from_email='robot@server.com',
recipient_list=[request.user.email],
context=extra_context)
我只想这么做。
编辑:这是我的视图:
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 (here i want to catch the click event):
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')
return render(request,template, extra_context)
您应该在
views.py
中创建此函数,将其映射到urls.py
中的url,并使用JavaScript(纯JS或使用jQuery
)添加事件处理程序,如下所示:JS(使用
jQuery
):HTML格式:
Python:
注意,如果您要使用
POST
方法,您应该关心csrf
(跨站点请求伪造)保护,如HERE所述相关问题 更多 >
编程相关推荐