Adding request.GET parameters to server side request

Adding request.GET parameters to server side request

sbarnettsbarnett Posts: 23Questions: 5Answers: 0

I'm trying to create a page which contains a form for searching the database (for events) as well as a list of the relevant events found.

I'm using server side processing to limit the load on each request but, since the call to the URL for the server side events is a separate HTTP request, the parameters used when someone submits the form aren't passed through to the server so I always get all events, regardless of the filtering done by the form.

The URL of the page (after the search form has been submitted) is:-

/events/?from_date=22%2F09%2F2016

I then create the datatable like this (to try and pass the GET parameters from the search into the server for processing):-

ajax: {
url: "{% url 'event_list' %}",
data: function (d) {
d.filters = "{{ request.GET|safe }}";
}
}

But that doesn't seem to work.

Is there a better way or am I on the right track and just need to make some small adjustment somewhere?

Answers

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    Is the problem basically that you don't know how to get the query string parameter in Javascript? This SO thread will help with that.

    Allan

  • sbarnettsbarnett Posts: 23Questions: 5Answers: 0

    Well, ideally, I wanted to pass all of the query string parameters in as a dictionary, so that my python code on the server side can analyse them. That way, I won't have to change the javascript in the template every time I update the form fields.

    It looks like that URL you posted is just explaining how to send through each parameter you want, separately.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    I'm slightly confused I'm afraid. Do you want to sent the parameters to the server when you make the Ajax request? And you want them to be in a JSON object?

    d.filters = JSON.stringify( myObject );
    

    Allan

  • sbarnettsbarnett Posts: 23Questions: 5Answers: 0
    edited September 2016

    In that case, I think my problem might be more that I don't know how to turn the request.GET variable (I'm using django - it's a context variable) into a javascript object so I can use the line of code you've posted above.

    If I just use:-

    JSON.stringify("{{ request.GET }}");
    

    it gives me:-

    "<QueryDict: {u'from_date': [u'29/09/2016'], u'to_date': [u'07/09/2016'], u'submit': [u'Submit']}>"
    

    which doesn't look right to me as I can't parse that as json.

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin

    In that case, I think my problem might be more that I don't know how to turn the request.GET variable

    I'm afraid you'd need to ask in a Django forum or StackOverflow. I have no experience of Django I'm afraid.

    Allan

This discussion has been closed.