简体   繁体   English

使用 Django 通过 AJAX URL 传递参数

[英]Passing a parameter through AJAX URL with Django

Below is my code.下面是我的代码。 'n' logs correctly in the console, and everything works perfectly if I manually enter the value for 'n' into url: '{% url "delete_photo" iddy=2%}' . 'n' 在控制台中正确记录,如果我手动将 'n' 的值输入到url: '{% url "delete_photo" iddy=2%}' ,一切都可以正常工作。 Alas, when I try to use 'n' as a variable (seen below) it gives me a reverse match not found error.唉,当我尝试使用 'n' 作为变量(见下文)时,它给了我一个反向匹配未找到的错误。 Can anyone help with this?有人能帮忙吗?

javascript javascript

 function test(n){
    console.log(n);
    $.ajax({
      type: 'get',
      url: '{% url "delete_photo" iddy=n%}',

      datatype:'json',
      success: function(data){
                alert(n)
                console.log(data)
                console.log(n)
                },
      error: console.log("SSS"),

    });}

html html

{% for t in photos %}
<div id="photobox" ><img id="img_pb3"src="{{t.photo.url}}">
<div><a><span onclick="test({{t.id}})" class="closeBtn">&times;</span></div></a>
</div>
{% endfor %}

urls网址

urlpatterns = [
     path('', views.explore, name='explore'),
     path('home/', views.home, name='home'),
     path('home/comment', views.comment, name='comment'),
     path('home/photo_del/<iddy>', views.delete_photo, name='delete_photo')
]

views意见

def delete_photo(request, iddy):

    data = {'a':iddy, 'b': iddy}
    return JsonResponse(data, safe=False)

You can't possibly do this.你不可能这样做。 You have fundamentally misunderstood the relationship between backend Django code and front-end Javascript code.您从根本上误解了后端 Django 代码和前端 Javascript 代码之间的关系。 Django templates are fully evaluated on the server, at which point the template tags are converted into plain HTML text. Django 模板在服务器上被完全评估,此时模板标签被转换为纯 HTML 文本。 There is therefore no way that a Javascript function - which runs much, much later, on the browser itself - can pass a parameter to be used in a template tag.因此,Javascript 函数(在浏览器本身上运行很久以后)无法传递要在模板标签中使用的参数。

The only way to do this would be to render the URL with a dummy value - one you know can't occur in the URL itself - and then replace that value in the JS with the one from the parameter, using normal JS string replace functions.做到这一点的唯一方法是用一个虚拟值呈现 URL——你知道这个值不会出现在 URL 本身中——然后使用普通的 JS 字符串替换函数用参数中的值替换 JS 中的值.

To be honest, it would be better to remove the ID from the URL altogether.老实说,最好从 URL 中完全删除 ID。 Apart from anything else, a delete action should be a POST, not a GET - you don't want the Googlebot accidentally crawling your URLs and deleting all your items - and with a POST you can send the ID in the posted data.除此之外,删除操作应该是 POST,而不是 GET - 您不希望 Googlebot 意外抓取您的网址并删除您的所有项目 - 并且通过 POST 您可以在发布的数据中发送 ID。

Maybe something like this:也许是这样的:

    <a class="closeBtn" href="{% url 'delete_photo' iddy=t.id %}" id="{{t.id}}"></a>

And then you can retrieve the reference using attr:然后您可以使用 attr 检索引用:

$.ajax({ 
url: $('.closeBtn').attr("href"),
data: { "iddy": $('.closeBtn').attr("id")},
.....

}) })

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

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