将变量从 js 发送到 Django

问题描述

我正在制作一个新闻应用程序,我必须保留用户点击的标题列表。如何将列表从 js 发送到 views.py (Django) 中的函数? 这是我的 js 代码及其工作,除了 ajax 部分。

var array_news = []
var final
function reply_click(clicked_id)
  {
     var title = clicked_id
     array_news.push(title)
     //alert(title)
     final = title
     alert(final)
     //return array_news
  }


function print_element(){
    if (array_news.length === 0 )
        alert("Is empty!")
    else
       
        $.ajax(
    {
        type:"GET",url: "validate",data:{ "post_id" : final }
     })

}

视图.py:

def validate(request):

if request.method == 'GET':
    post_id = request.GET.get('post_id',None)
    print(post_id)
    return HttpResponse("Success.")
    
else:
    print(request.method)
    return HttpResponse("Request method is not a GET")

urls.py:

path(r'validate$',views.validate,name="validate"),

ajax 结果没有返回。 我尝试了各种解决方案,但我不知道。你能帮我吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)