简体   繁体   English

在多个 url django 上调用相同的函数

[英]Call same function on multiple urls django

urls.py网址.py

 url(r'^level/ajax/reload/$', views.ajax_change_status, 
 name='ajax_change_status'),
 url(r'^level/(\d+)/ajax/reload/$', views.ajax_change_status, 
 name='ajax_change_status'),
 url(r'^level/(\d+)/(\d+)/ajax/reload/$', views.ajax_change_status, 
 name='ajax_change_status'),

In my urls.py i have these urls.Im trying to call an ajax function in my view basically to update the notification badge to 0 after user clicks on the bell icon.The notification badge is in the base.html template.Im calling the url with the name "ajax_change_status" .在我的 urls.py 中,我有这些 urls。我试图在我的视图中调用一个 ajax 函数,基本上是在用户点击铃铛图标后将通知徽章更新为 0。通知徽章在 base.html 模板中。我正在调用url 名称为 "ajax_change_status" 。 I want all these urls to call the same ajax funtion.Is it possible to do this or is there a better way?Im getting a 500 server error when i click on the bell icon from the second and third url我希望所有这些 url 都调用相同的 ajax 功能。是否可以这样做或有更好的方法?当我单击第二个和第三个 url 中的铃铛图标时,我收到 500 服务器错误

My ajax function in views.py:我在 views.py 中的 ajax 函数:

def ajax_change_status(request):
  if request.is_ajax():
   try:
     Notification.objects.filter(receiver=request.user)
     .update(viewed=True)
     Addnotify.objects.filter(receiver=request.user)
     .update(viewed=True)
     FollowNotify.objects.filter(receiver=request.user)
     .update(viewed=True)
     HubNotify.objects.filter(receiver=request.user)
     .update(viewed=True)
    return JsonResponse({"success": True})
   except Exception as e:
      print(e)
      return JsonResponse({"success": False})

My ajax jquery:我的 ajax jquery:

var clicks = 0;

 $("#notify").on('click', function () {

    $.ajax({
    url: 'ajax/reload/',
    data: {
     },

    success: function (data) {

      if (data.success) {
                    console.log('ajax call  success.');

        $('#badge').html('0')
        $('#headnotify').html('NOTIFICATIONS (0)')
        // here you update the HTML to change the active to innactive
      }else{
        console.log('ajax call not success.');
      }
    clicks++;
    }
    });
   });

Seems like it is not getting into the ajax function when i try it on the second and third url!!当我在第二个和第三个 url 上尝试时,它似乎没有进入 ajax 功能!!

Each one of those urls should have different names.这些网址中的每一个都应该有不同的名称。 It's going to be pain to allow for optional url arguments.允许可选的 url 参数会很痛苦。 You'll be much better off (and more sane) if you just name them differently.如果你只是给它们起不同的名字,你会过得更好(也更理智)。 Your view function probably should allow for those to be passed in though, otherwise what's the purpose of them?您的视图函数可能应该允许传入这些函数,否则它们的目的是什么?

def ajax_change_status(request, param1=None, param2=None):
    ...

And personally I like using keyword arguments as it better explains the url path, but that's up to you.我个人喜欢使用关键字参数,因为它可以更好地解释 url 路径,但这取决于您。

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

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