简体   繁体   English

如何将带有参数的 django url 发送到 ajax 请求?

[英]How to send django url with parameters to ajax request?

Currently, I am sending url to the ajax as:目前,我将 url 发送到 ajax 为:

    $('.togglebtn').on("click",function(){
         console.log("Hello")
         $.ajax({
            type: 'GET',
            url: "http://localhost:8000/toggle/2945016571198",
            contentType: 'application/json',
            success: function (data) {
                  appendData(data);
                   function appendData(data) {
            var mainContainer = document.getElementById("switch_{{forloop.counter}}");
            for (var i = 0; i < data.length; i++) {
                var div = document.createElement("div");
                div.innerHTML = '<tr>' + data[i].line_items + ' ' + data[i].nomenclature+'</tr>' ;
                mainContainer.appendChild(div);
            }
        }
            }
  });

Currently, I am sending request into a static url.目前,我正在向 static url 发送请求。 Can we use {% url toggle i.id %} to set the url dynamically in the ajax request?我们可以使用 {% url toggle i.id %} 在 ajax 请求中动态设置 url 吗? Does this ajax function repeats at all or not?这个 ajax function 是否重复?

Not in that way or fashion, the {% url toggle i.id %} is for the Django template, so that is executed when rendering the page server side.不是以这种方式或方式, {% url toggle i.id %}用于 Django 模板,因此在呈现页面服务器端时执行。 What you want to do is client side.你想做的是客户端。

Your are also trying to use "switch_{{forloop.counter}}" , which won't work unless you have multiple snippets of the 'click' function.您还尝试使用"switch_{{forloop.counter}}" ,除非您有多个“点击”function 片段,否则它将不起作用。 Which I would advise against since it simply doesn't make sense.我建议不要这样做,因为它根本没有意义。 You define a function once and then use it.您定义一次 function 然后使用它。 See first example.见第一个例子。

Best thing I can think of is exposing a 'base url' in your template and use that in javascript.我能想到的最好的事情是在您的模板中公开一个“基本网址”并在 javascript 中使用它。 For example in your Django template:例如在您的 Django 模板中:

<script>
// Put the Django variable into javascript context.
toggle_base_url = {{ toggle_base_url_from_django_view_context }};
// Use it in your ajax url by combining strings.
 $('.togglebtn').on("click",function(){
         console.log("Hello")
         var obj = $(this); // Save the clicked button for reference.
         var objectId = obj.attr('id');
         var buttonId = obj.attr('data-button-id');
         $.ajax({
            type: 'GET',
            url: toggle_base_url + "/" + buttonId
            contentType: 'application/json',
            success: function (data) {
                // Cannot use 'this' in this context (IIRC), use the obj var we assigned.
                // Replace with something to find your parent container
                // Could use have a attribute on your button that points to the container.
                // Probably the number you use in the url?
                var mainContainer = obj.parent('containerclass');
                for (var i = 0; i < data.length; i++) {
                    var div = document.createElement("div");
                    div.innerHTML = '<tr>' + data[i].line_items + ' ' + data[i].nomenclature+'</tr>' ;
                    mainContainer.appendChild(div);
                }
            }
        }
  });

</script>

In your Django view you do something like this:在您的 Django 视图中,您可以执行以下操作:


# Make a base url to reuse, remove the last part.
# So turn `/toggle/1` into `/toggle`
base_url = reverse('toggle', args=[1]).rsplit('/', 1)
context = {
    'toggle_base_url_from_django_view_context': base_url,
}
return render(request, 'yourtemplate.html', context=context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM