django / ajax:无法在views.py中获取Ajax发布数据

[英]django/ajax: Unable to get Ajax post data in the

I had a problem getting ajax post data from django backend, I don't know how to pass the value, please help.


In html I have simply this:


<form id="get_vulns_from_family">
    <label for="family_content">Enter a family name to display the NVTs</label>
    <input id="family_content" />
    <input type="submit" value="search" />

In javascript I wrote this:


$(function() {
    $("#get_vulns_from_family").submit(function(event) {
        var family_text = $("#family_content").val();
        var family_data = {"family": family_text};
            url: "/template_conf/get_vulns_from_family",
            type: "POST",
            data: family_data,
            success: function(response) {
            error: function(response) {
        // prevent default posting of form

In Django method corresponding to url /template_conf/get_vulns_from_family, I tried this:

在对应于url / template_conf / get_vulns_from_family的Django方法中,我试过这个:

def get_vuln_from_family(request):
    family = request.POST['family']
    # some other operations to get value for variable "json_data"
    return HttpResponse(simplejson.dumps(json_data))

But django said: MultiValueDictKeyError: "Key 'family' not found in <QueryDict: {}>", which means the POST dictionary is empty. Am I using the wrong way to get post data? If so what should I do? Thanks.

但django说:MultiValueDictKeyError:“ ”中找不到“键'系列',这意味着POST字典为空。我使用错误的方式获取发布数据吗?如果是这样我该怎么办?谢谢。

2 个解决方案



your url "/template_conf/get_vulns_from_family" is missing a trailing slash. django will typically redirect this to "/template_conf/get_vulns_from_family/", dropping the POST data

您的网址“/ template_conf / get_vulns_from_family”缺少一个尾部斜杠。 django通常会将其重定向到“/ template_conf / get_vulns_from_family /”,删除POST数据



If your CSRF enabled then simple ajax post do not work. you will have to add the csrf token and set it to the ajax request header.


For Ajax POST request, you have to pass the CSRF token in as POST data with every POST request. For this reason, you must get CSRF token first. Since you have enabled CSRF protection so you will get the token from csrftoken cookie. The CSRF token cookie is named csrftoken by default. Acquiring the token is very straight forward and it can be achieved using the below code snippet.

对于Ajax POST请求,您必须在每次POST请求时将CSRF令牌作为POST数据传递。因此,您必须首先获得CSRF令牌。由于您已启用CSRF保护,因此您将从csrftoken cookie获取令牌。默认情况下,CSRF令牌cookie名为csrftoken。获取令牌非常简单,可以使用下面的代码片段来实现。

function getCookie(name) {  
    var cookieValue = null;  
    if (document.cookie && document.cookie != '') {  
        var cookies = document.cookie.split(';');  
        for (var i = 0; i < cookies.length; i++) {  
            var cookie = jQuery.trim(cookies[i]);  

            if (cookie.substring(0, name.length + 1) == (name + '=')) {  
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
    return cookieValue;  

function csrfSafeMethod(method) {  

    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));  

function sameOrigin(url) {  

    var host =; // host + port  
    var protocol = document.location.protocol;  
    var sr_origin = '//' + host;  
    var origin = protocol + sr_origin;  

    return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||  
        (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||  


$(function() {  



                success: function(){  
                    $('#message').html("<h2 style='color:green;'>Person Form Submitted!</h2>")  
                error: function(){  
                    $('#message').html("<h2 style='color:red;'>Can't submit form</h2>")  
            return false;  

        beforeSend: function(xhr, settings) {  
            if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {  
                // Send the token to same-origin, relative URLs only.  
                // Send the token only if the method warrants CSRF protection  
                // Using the CSRFToken value acquired earlier  

                xhr.setRequestHeader("X-CSRFToken", csrftoken);  



© 2014-2019 粤ICP备14056181号