0

I am developing a django application in which I am trying to send a value of the variable to the backend on click of a button through javascript.

javascript code:

$(document).on("click", "#filter", function (e) {
      IUPredscorethreshold = 0.4
      $("#ksNetwork").empty();
      ksInteractionNetwork('{% url "camkinetv2:newinteractors_intnet" tab1.caMKipedia_Id IUPredscorethreshold %}');
 });

urls.py

path(
        "dataJson/newinteractors_intnet/<str:geneId>/<str:IUPredscorethreshold>",
        views.newinteractors_intnet,
        name="newinteractors_intnet",
    ),

views.py

@csrf_exempt
def newinteractors_intnet(request, geneId, IUPredscorethreshold):

    print("IUPredscorethreshold:" + IUPredscorethreshold)

.
.
.
.
.
some computation


    graphData = {"nodes": uniquenodesdata, "links": linksdata}

    response = JsonResponse(graphData)

    return response

when I execute this code i am getting following error:

NoReverseMatch at /v2/search/SARS_CoV_2/GID1716
Reverse for 'newinteractors_intnet' with arguments '('GID1716', '')' not found. 1 pattern(s) tried: ['v2/dataJson/newinteractors_intnet/(?P<geneId>[^/]+)/(?P<IUPredscorethreshold>[^/]+)$']
Exception Value:    
Reverse for 'newinteractors_intnet' with arguments '('GID1716', '')' not found. 1 pattern(s) tried: ['v2/dataJson/newinteractors_intnet/(?P<geneId>[^/]+)/(?P<IUPredscorethreshold>[^/]+)$']

what am I doing wrong? how can I solve this issue. I am still at learning stage of django and I am not able to figure out how to solve this error.

Henrique Forlani
  • 1,325
  • 9
  • 22
K.M.J
  • 105
  • 1
  • 2
  • 13

3 Answers3

1

The error message says it couldn't find a url that matches {% url "camkinetv2:newinteractors_intnet" tab1.caMKipedia_Id IUPredscorethreshold %}. Your urls.py declares IUPredscorethreshold to be a string by putting str: in front of it. In your javascript you assign 0.4 to that variable which is an integer. Therefore, the url you doesn’t resolve.

Change your path in urls.py to: path( "dataJson/newinteractors_intnet/<str:geneId>/<int:IUPredscorethreshold>", views.newinteractors_intnet, name="newinteractors_intnet", ),

Vincent
  • 1,494
  • 12
  • 26
  • I had made changes to urls.py file but still getting the error: `Reverse for 'newinteractors_intnet' with arguments '('GID1716', '')' not found. 1 pattern(s) tried: ['v2/dataJson/newinteractors_intnet/(?P[^/]+)/(?P[0-9]+)$'] ` – K.M.J Jun 22 '20 at 06:11
  • moreover what I have observed is, if i change my javascript line from `ksInteractionNetwork('{% url "camkinetv2:newinteractors_intnet" tab1.caMKipedia_Id IUPredscorethreshold %}');` to `ksInteractionNetwork('{% url "camkinetv2:newinteractors_intnet" tab1.caMKipedia_Id "IUPredscorethreshold" %}')` i.e. putting `IUPredscorethreshold in "IUPredscorethreshold"` i am not getting any error but in my views.py when I print it I am getting it variable not the value `IUPredscorethreshold:IUPredscorethreshold` – K.M.J Jun 22 '20 at 06:24
0

Change your Javascript code to this:

$(document).on("click", "#filter", function (e) {
  IUPredscorethreshold = 0.4
  $("#ksNetwork").empty();
  let url = '{% url "camkinetv2:newinteractors_intnet" ' + tab1.caMKipedia_Id + ' ' + IUPredscorethreshold + '%}'
  ksInteractionNetwork(url);
});

I believe that the url is not forming correctly

  • I had changed the javascript but I am still getting an error: ` Reverse for 'newinteractors_intnet' with arguments '(' + tab1.caMKipedia_Id + ', ' + IUPredscorethreshold + ')' not found. 1 pattern(s) tried: ['v2/dataJson/newinteractors_intnet/(?P[^/]+)/(?P[0-9]+)$'] ` – K.M.J Jun 22 '20 at 06:02
0

changing javascript line from:ksInteractionNetwork('{% url "camkinetv2:newinteractors_intnet" tab1.caMKipedia_Id IUPredscorethreshold %}'); to ksInteractionNetwork('{% url "camkinetv2:newinteractors_intnet" tab1.caMKipedia_Id "IUPredscorethreshold" %}'.replace(/IUPredscorethreshold/, IUPredscorethresholdvalues) );

following answer helped answer

K.M.J
  • 105
  • 1
  • 2
  • 13