简体   繁体   English

Django request.GET.get() 每两次返回 None

[英]Django request.GET.get() returns None every two times

I'm trying to use some ajax request to echange between django views and the templates but i'm experiencing a strange behavior with the request.GET.我正在尝试使用一些 ajax 请求在 django 视图和模板之间进行交换,但我遇到了 request.GET 的奇怪行为。
Django sends an error saying that the data in parameter of the json.loads(data) shouldn't be NoneType as the result of request.GET.get('selects') seems to be None ; Django 发送一个错误,说json.loads(data)参数中的json.loads(data)不应该是NoneType作为request.GET.get('selects') selects request.GET.get('selects')似乎是None but if I try to debug this code, it seems that request.GET.get('selects') returns None every two times.但是如果我尝试调试这段代码, request.GET.get('selects')似乎每两次返回None

When requesting request.GET.get('selects') in the console, I get:在控制台中请求request.GET.get('selects')时,我得到:
None the first time第一次没有
and '[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]' the second time, '[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]' 第二次,
then None again然后没有了
then '[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]' at the third attempt.然后 '[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]' 在第三次尝试时。
every odd attempt return None.... what do I wrong ?每一次奇怪的尝试都返回 None .... 我错了什么?

views.py视图.py

class AjaxRenameView(View):

    def get(self,request):
        #import pdb; pdb.set_trace()
        selects=request.GET.get('selects', None)
        selects=json.loads(selects)
        pks = [ pk['pk'] for pk in selects]
        if len(pks)==1:
            folder=Folder.objects.filter(pk=pks[0])
            spotler=Spotler.objects.filter(pk=pks[0])
            if folder:
                form=FolderRenameForm(instance=folder)
                title=_('rename folder')
            elif spotler :
                form=SpotlerRenameForm(instance=folder)
                title=_('rename project')

            context={'form':form,'title':title,'submit':_('rename'),'has_error':False}
            html_form = render_to_string('includes/modal_form.html',
                context,
                request=request,
            )
            return JsonResponse({'html_form': html_form})

        return JsonResponse({'has_error':True, 'message':_('an error has occured while renaming')}, safe=False)

    def post(self,request):
        pass

folder.js文件夹.js

  //...
  function ajaxAction(selects,url,next){
    // when clicked on item of menu (rename, share, move...)
    $.ajax({
      type:"GET",
      data:{'selects':JSON.stringify(selects),'next':next},
      url: url,
      dataType:"json",
      beforeSend: function () {
        $("#Modal .modal-content").empty();
      },
      success: function (data) {
        $("#Modal .modal-content").html(data.html_form);
      }
    });//ajax
  }//function ajaxAction
//...

console安慰

(Pdb) request.GET.dict()
{'selects': '[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]', 'next': '/fr/wf/myfiles/'}
(Pdb) request.GET.get('selects')
(Pdb) request.GET.get('selects')
'[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]'
(Pdb) request.GET.get('selects')
(Pdb) request.GET.get('selects')
'[{"pk":"57226796-0960-428a-88aa-ba4120ad34b4"}]'

well I finally found it.好吧,我终于找到了。 the click() event that triggered the ajax call was related to the href attribute of an element.触发 ajax 调用的 click() 事件与元素的 href 属性有关。 And as I didn't halt the execution of the function, the link was activated and a call to the django view without parameters was send... the ajax call with return false;由于我没有停止函数的执行,链接被激活,并发送了对 django 视图的无参数调用...... return false;的 ajax 调用return false; or e.preventDefault();e.preventDefault();

I modified the ajax call a little bit :我稍微修改了 ajax 调用:

function ajaxActionMenu(pk_list,url){
  $.ajax({
    type:'GET',
    data:{'pk_list':JSON.stringify(pk_list)},
    url: url,
    dataType:'json',
    success:function(data){
      jQuery.each(data, function(index,value){
        $("#Modal .modal-body .nav").append('<a class="nav-link" href="'+value["url"]+'">'+value["menuitem"]+'</a>');
      });
    },
    complete: function(){
      $("#Modal .modal-body .nav").on("click",".nav-link",function(e){
        e.preventDefault(); // prevent the activation of the href link
        var urlLink=$(this).attr("href");
        ajaxAction(pk_list,urlLink);
        $("#Modal").modal("show");
      });

    }
  });
}

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

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