Javascript Django CSRF令牌丢失

Javascript Django CSRF令牌丢失,javascript,jquery,python,ajax,django,Javascript,Jquery,Python,Ajax,Django,我在custom.js文件中有一个函数,如下所示: function contactTraxio(fullname, telephone, email) { if (typeof(fullname)==='undefined') fullname = null; if (typeof(telephone)==='undefined') telephone = null; if (typeof(email)==='undefined') email = null;

我在custom.js文件中有一个函数,如下所示:

function contactTraxio(fullname, telephone, email) {
    if (typeof(fullname)==='undefined') fullname = null;
    if (typeof(telephone)==='undefined') telephone = null;
    if (typeof(email)==='undefined') email = null;
    bootbox.dialog({
        title: "Limit reached",
        message: '<p class="text-main text-center">You have reached the limit of your calculations.</p>' +
                 '<p class="pad-btm mar-btm text-center">Upgrade your account by contacting us on +32 9 111 12 12 or filling in the form below.</p>' +
                 '<div class="row"> ' +
                    '<div class="col-md-12"> ' +
                        '<form class="" method="POST"> ' +

                            '<div class="form-group"> ' +
                                '<div class="col-md-6" style="padding-left: 0"> ' +
                                    '<input id="contact-fullname" name="fullname" type="text" placeholder="Your fullname" class="form-control input-md" value="' + fullname + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                                '<div class="col-md-6" style="padding-right: 0"> ' +
                                    '<input id="contact-telephone" name="telephone" type="text" placeholder="Telephone" class="form-control input-md" value="' + telephone + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                                '<div class="col-md-12 pad-no-lr-md" style="margin-top: 7.5px;"> ' +
                                    '<input id="contact-email" name="email" type="text" placeholder="Your email address" class="form-control input-md" value="' + email + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                            '</div>' +
                        '</form> ' +
                    '</div>' +
                 '</div>',
        buttons: {
            success: {
                label: "Send",
                className: "btn-primary",
                callback: function () {
                    $.ajax({
                       type: 'POST',
                       url: '/master/contact_traxio/',
                       data: {
                           fullname: $('#contact-fullname').val(),
                           telephone: $('#contact-telephone').val(),
                           email: $('#contact-email').val(),
                           csrfmiddlewaretoken: '{{ csrf_token }}'
                       },
                       success: function (data) {
                           debugger;
                       }
                    });


                }
            }
        }
    });
}
@login_required
def contact_traxio(request):
    if request.method == 'POST':
        # Just test
        return HttpResponse('{} / {} / {}'.format(request.POST['fullname'], request.POST['telephone'], request.POST['email']))
    else:
        return HttpResponseBadRequest("Sorry. Something went wrong.")
{% block page_content %}
    <script>
        $(document).ready(function () {
            var fullname = '{{ user.user.first_name }} {{ user.user.last_name }}';
            contactTraxio(fullname, '{{ user.telephone }}', '{{ user.user.email }}')
        })
    </script>
{% endblock %} 
contact_traxio视图如下所示:

function contactTraxio(fullname, telephone, email) {
    if (typeof(fullname)==='undefined') fullname = null;
    if (typeof(telephone)==='undefined') telephone = null;
    if (typeof(email)==='undefined') email = null;
    bootbox.dialog({
        title: "Limit reached",
        message: '<p class="text-main text-center">You have reached the limit of your calculations.</p>' +
                 '<p class="pad-btm mar-btm text-center">Upgrade your account by contacting us on +32 9 111 12 12 or filling in the form below.</p>' +
                 '<div class="row"> ' +
                    '<div class="col-md-12"> ' +
                        '<form class="" method="POST"> ' +

                            '<div class="form-group"> ' +
                                '<div class="col-md-6" style="padding-left: 0"> ' +
                                    '<input id="contact-fullname" name="fullname" type="text" placeholder="Your fullname" class="form-control input-md" value="' + fullname + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                                '<div class="col-md-6" style="padding-right: 0"> ' +
                                    '<input id="contact-telephone" name="telephone" type="text" placeholder="Telephone" class="form-control input-md" value="' + telephone + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                                '<div class="col-md-12 pad-no-lr-md" style="margin-top: 7.5px;"> ' +
                                    '<input id="contact-email" name="email" type="text" placeholder="Your email address" class="form-control input-md" value="' + email + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                            '</div>' +
                        '</form> ' +
                    '</div>' +
                 '</div>',
        buttons: {
            success: {
                label: "Send",
                className: "btn-primary",
                callback: function () {
                    $.ajax({
                       type: 'POST',
                       url: '/master/contact_traxio/',
                       data: {
                           fullname: $('#contact-fullname').val(),
                           telephone: $('#contact-telephone').val(),
                           email: $('#contact-email').val(),
                           csrfmiddlewaretoken: '{{ csrf_token }}'
                       },
                       success: function (data) {
                           debugger;
                       }
                    });


                }
            }
        }
    });
}
@login_required
def contact_traxio(request):
    if request.method == 'POST':
        # Just test
        return HttpResponse('{} / {} / {}'.format(request.POST['fullname'], request.POST['telephone'], request.POST['email']))
    else:
        return HttpResponseBadRequest("Sorry. Something went wrong.")
{% block page_content %}
    <script>
        $(document).ready(function () {
            var fullname = '{{ user.user.first_name }} {{ user.user.last_name }}';
            contactTraxio(fullname, '{{ user.telephone }}', '{{ user.user.email }}')
        })
    </script>
{% endblock %} 
我在django模板中调用
contactTraxio
函数,如下所示:

function contactTraxio(fullname, telephone, email) {
    if (typeof(fullname)==='undefined') fullname = null;
    if (typeof(telephone)==='undefined') telephone = null;
    if (typeof(email)==='undefined') email = null;
    bootbox.dialog({
        title: "Limit reached",
        message: '<p class="text-main text-center">You have reached the limit of your calculations.</p>' +
                 '<p class="pad-btm mar-btm text-center">Upgrade your account by contacting us on +32 9 111 12 12 or filling in the form below.</p>' +
                 '<div class="row"> ' +
                    '<div class="col-md-12"> ' +
                        '<form class="" method="POST"> ' +

                            '<div class="form-group"> ' +
                                '<div class="col-md-6" style="padding-left: 0"> ' +
                                    '<input id="contact-fullname" name="fullname" type="text" placeholder="Your fullname" class="form-control input-md" value="' + fullname + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                                '<div class="col-md-6" style="padding-right: 0"> ' +
                                    '<input id="contact-telephone" name="telephone" type="text" placeholder="Telephone" class="form-control input-md" value="' + telephone + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                                '<div class="col-md-12 pad-no-lr-md" style="margin-top: 7.5px;"> ' +
                                    '<input id="contact-email" name="email" type="text" placeholder="Your email address" class="form-control input-md" value="' + email + '"> ' +
                                    '<span class="help-block"><small></small></span> </div> ' +
                                '</div> ' +
                            '</div>' +
                        '</form> ' +
                    '</div>' +
                 '</div>',
        buttons: {
            success: {
                label: "Send",
                className: "btn-primary",
                callback: function () {
                    $.ajax({
                       type: 'POST',
                       url: '/master/contact_traxio/',
                       data: {
                           fullname: $('#contact-fullname').val(),
                           telephone: $('#contact-telephone').val(),
                           email: $('#contact-email').val(),
                           csrfmiddlewaretoken: '{{ csrf_token }}'
                       },
                       success: function (data) {
                           debugger;
                       }
                    });


                }
            }
        }
    });
}
@login_required
def contact_traxio(request):
    if request.method == 'POST':
        # Just test
        return HttpResponse('{} / {} / {}'.format(request.POST['fullname'], request.POST['telephone'], request.POST['email']))
    else:
        return HttpResponseBadRequest("Sorry. Something went wrong.")
{% block page_content %}
    <script>
        $(document).ready(function () {
            var fullname = '{{ user.user.first_name }} {{ user.user.last_name }}';
            contactTraxio(fullname, '{{ user.telephone }}', '{{ user.user.email }}')
        })
    </script>
{% endblock %} 
{%block page\u content%}
$(文档).ready(函数(){
var fullname={{user.user.first_name}{{{user.user.last_name}}};
contactTraxio(全名,{{user.telephone}}},{{{user.user.email}}})
})
{%endblock%}
为什么没有发送csrf令牌


有什么建议吗?

您正在外部JS文件中使用Django模板语法。这是行不通的,因为Django不解析这些文件


文档显示如何从JS访问令牌;您应该遵循它。

您正在外部JS文件中使用Django模板语法。这是行不通的,因为Django不解析这些文件

文档显示如何从JS访问令牌;你应该遵循它