如何通过单击django中的按钮来调用视图中的函数?

时间:2016-06-13 18:15:11

标签: javascript jquery python html django

我试图从我的html模板调用函数ssl_verify。但它给出了404错误。谁能帮我这个?我哪里错了?

#views.py
def ssl_verify( request , dns, port ):

if request.is_ajax():

    result = dns + port  
    return result

#urls.py
url(r'ssl_verify/(\d+)/(\d+)/$', views.ssl_verify,name='ssl_verify'),

#script in html 
function verify()
{

dns = document.getElementById("dns1").value;

port = document.getElementById("port1").value;

 $.post('ssl_verify/'+dns+'/'+port+'/', function (data) {

          alert (data) ;

            });
          }

1 个答案:

答案 0 :(得分:0)

您的urls.py必须包含(替换正则表达式行)

url(r'^ssl_verify/(?P<dns>[^/]+)/(?P<port>[^/]+)/$','views.check_ssl'),

或者如果您导入:

from views import check_ssl

....

url(r'^ssl_verify/(?P<dns>[^/]+)/(?P<port>[^/]+)/$','check_ssl'),

和您的观点:

from django.http import HttpResponse

def check_ssl( request , dns, port ):

    if request.is_ajax():
        message = str(dns)+str(port)
        return HttpResponse(message)

    else:
        return  HttpResponse('/faulthandler') # declare in urls if needed