本文介绍了在Django中发送动态AJAX URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的ajax函数,效果很好:

I have ajax function like this which is working fine:

 function show_tasks() {
              $.ajax({
                        type: "GET",
                        url: "/2/todos/", //I want url: "/{{ category.id }}/todos/"
                        success: function(data){
                             $('#tasks').html(data)
                             .css("display", "block");
                        }
                    });
        }

HTML内容

{% for category in categories %}
    <tr>
    <td width="5%"><input type="checkbox" value="{{ category.id }}" name="category_name" id="category{{ forloop.counter }}" /></td>
    <td>
        <a data-toggle="tooltip" title="Show Tasks" onclick="show_tasks()">
            {{ category.name }}</a>

{% endfor %}

我该怎么做?

推荐答案

使用模板处理器在html中构造url,例如:

Construct url in html with template processor, something like:

...
<a data-toggle="tooltip" title="Show Tasks"
   onclick="show_tasks({% url 'category_todo' category.id %})">
...

并在您的javascript中添加网址支持:

and add url support in your javascript:

function show_tasks(url){...
   url: url,
   ...
}

这篇关于在Django中发送动态AJAX URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:16
查看更多