简体   繁体   English

ajax 调用不适用于 django

[英]ajax call is not working with django

i am creating a blog just for practice and i want to mark all notifications as seen when the user view the notifications, this is working if i go to the url but when i use ajax nothing happen我正在创建一个博客只是为了练习,我想在用户查看通知时将所有通知标记为所见,如果我转到 url,这是有效的,但是当我使用 ajax 时什么也没有发生

javascript: javascript:

<script>
$(document).ready(function() {
   $("#testb").click(function(event){
        $.ajax({
             type:"POST",
             url:"{% url 'seen' request.user.username %}",
             success: function(){
                 confirm('it worked')
             }
        });
        return false;
   });

});
</script>

url url(r'functions/user/(?P<username>\\w+)/seen', views.seen, name='seen') url url(r'functions/user/(?P<username>\\w+)/seen', views.seen, name='seen')

view:看法:

def seen(request, username):
    nat = Nat.objects.filter(toUser=username)
    nat.update(show=False)
    return HttpResponse("test")
@csrf_exempt
def seen(request, username):
    if request.method=='POST':
        nat = Nat.objects.filter(toUser=username)
        nat.update(show=False)
        return JsonResponse("test":"json response")

Try this, it is not safe for production but as you said, for just training is useful.试试这个,它对生产不安全,但正如你所说,因为只有培训才有用。

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

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