从 django 视图中的按钮获取点击事件 [英] Get the click event from a button in a django view

查看:87
本文介绍了从 django 视图中的按钮获取点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题很清楚.我想知道用户何时单击按钮以在我的 views.py 中的函数中运行一段代码.假设我有这个 html:

<input type="button" name="_mail" value="Enviar Mail">

如果用户点击它,我想运行它:

send_templated_mail(template_name='receipt',from_email='robot@server.com',收件人列表=[request.user.email],上下文=额外的上下文)

这就是我想要做的.

编辑:这是我的观点:

def verFactura(request, id_factura):事实 = Factura.objects.get(pk = id_factura)客户 = Cliente.objects.get(factura = fact)模板 = 'verfacturas.html'iva = fact.importe_sin_iva * 0.21总计 = fact.importe_sin_iva + ivaextra_context = dict()extra_context['事实'] = 事实extra_context['cliente'] = 客户extra_context['iva'] = ivaextra_context['total'] = 总计如果(在这里我想捕捉点击事件):send_templated_mail(template_name='receipt',from_email='imiguel@exisoft.com.ar',收件人列表 =['ignacio.miguel.a@gmail.com'],上下文=额外的上下文)返回 HttpResponseRedirect('../facturas')返回渲染(请求,模板,extra_context)

解决方案

你应该在你的 views.py 中创建这个函数,将它映射到你的 urls.py 中的 urlcode> 并使用 JavaScript 添加事件处理程序(纯 JS 或使用 jQuery 如下所示):

JS(使用 jQuery):

$('#buttonId').click(function() {$.ajax({网址:your_url,方法:'POST',//或其他(GET),无论您需要什么数据: {name: value,//需要传递给函数的数据点击:真实}成功:功能(数据){//成功回调//可以处理views.py中函数返回的数据}});});

HTML:

<input type="button" id="buttonId" name="_mail" value="Enviar Mail">

蟒蛇:

def verFactura(request, id_factura):...if request.POST.get('click', False): # 检查是否被点击调用# 发送邮件等...

请注意,如果您打算使用 POST 方法,您应该关注 csrf(跨站点请求伪造)保护,如这里

i think the title is pretty clear. I want to know when the user clicks the button to run a piece of code in a function in my views.py. lets say i have this html:

<div>
    <input type="button" name="_mail" value="Enviar Mail">  
</div>

and i want to run this code if the user clicks on it:

send_templated_mail(template_name='receipt',
                    from_email='robot@server.com',
                    recipient_list=[request.user.email],
                    context=extra_context)

that´s all i want to do.

EDIT: this is the view that i have:

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)

解决方案

You should create this function in your views.py, map it to the url in your urls.py and add event handler using JavaScript (pure JS or using jQuery as shown below):

JS (using jQuery):

$('#buttonId').click(function() {    
    $.ajax({
        url: your_url,
        method: 'POST', // or another (GET), whatever you need
        data: {
            name: value, // data you need to pass to your function
            click: true
        }
        success: function (data) {        
            // success callback
            // you can process data returned by function from views.py
        }
    });
});

HTML:

<div>
    <input type="button" id="buttonId" name="_mail" value="Enviar Mail">  
</div>

Python:

def verFactura(request, id_factura):

    ...    

    if request.POST.get('click', False): # check if called by click
        # send mail etc.        

    ...

Note that if you're going to use POST method you should care about csrf (cross-site request forgery) protection as described HERE

这篇关于从 django 视图中的按钮获取点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆