MultiValueDictKeyError at /backendurl/

Submitted 4 years, 5 months ago
Ticket #285
Views 702
Language/Framework Django
Priority Medium
Status Closed

Hi,

I am trying to get the time spent on each page in a django project using Javascript. I keep getting an error so I am trying to fix it

the error:

MultiValueDictKeyError at /backendurl/
'timeSpent'

Highlighted line of error in views.py:

    timeSpent = request.POST['timeSpent'] 

Here is the javascript in template:

<script>
$(document).ready(function() {
  var startDate = new Date();

  $(window).unload(function() {
      var endDate = new Date();
      $.ajax({
        type: POST,
        url: "backendurl", // be mindful of url names
        data: {
          'timeSpent': endDate - startDate || 'none' // defaults to a string
        },
        success: function(response) {

          // unpack response:
          status = response.status

        }
      })
</script>

Here is the views.py:

def my_view_function(request):
    timeSpent = request.POST['timeSpent']
    response = json.dumps({
        'timeSpent' : timeSpent,
    })
    return HttpResponse(response)

Here is the url.py

    path('backendurl/', views.my_view_function, name='backendurl'),

Submitted on Nov 06, 20

@ahmed, will check and let you know. - Vengat 4 years, 5 months ago

what are the value you are getting here request.POST['timeSpent'] or change this to request.POST.get('timeSpent') - Vengat 4 years, 5 months ago (Edited)

Can you let me know the above question which i aasked? So that it would be helpful for me to solve your ticket.? - Vengat 4 years, 5 months ago

@Arun, I am getting this error django.utils.datastructures.MultiValueDictKeyError: 'timeSpent' - ahmed.hisham87 4 years, 5 months ago

can we connect & take a look on this issue?. - Vengat 4 years, 5 months ago

as we dont get any response on this ticket?. we are closing from our end. - Vengat 4 years, 4 months ago
add a comment


Latest Blogs