Question

I'm trying to implement an ajax function that will execute a database query based on the id value of a drop down selection.

The HTML of the drop down list is

<form method = "POST" action="" >{% csrf_token %}
  <select name = "parentorgs" id = "parentorgs">
    {% for org in parentorg_list %}
      <option value = "{{org.parentorg}}" id = "{{org.parentorg}}" >{{ org.parentorgname }}</option>
    {% endfor %}
  </select>
</form>

A jQuery change() function is used to get the ID of the selection and passes it to

function getData(id) {
  $.ajax({
    type : "POST",
    url : "getData/",
    data : {"parentorg" : id},
    datatype: "json",
    success : function(data) {
      console.log(data)
}
  });
}

which in turn calls the view function

from django.shortcuts import render_to_response, render
from django.core.context_processors import csrf

def getData(request):
  c = {}
  c.update(csrf(request))
  return render_to_response("app/index.html", c)

Firebug shows that the request is going through via POST, and the method URL is valid. In addition, the URL of this method has been added to urls.py.

At this time, its not doing anything, as I just want to see the response from the method. This method is intended to execute a model query and return the results.

Each time an item is selected in the dropdown, I get an error 403 describing that the view uses ResponseContext rather than Context for the template.

What needs to be done to resolve this issue?

Was it helpful?

Solution

According to the doc

If you’re using Django’s render_to_response() shortcut to populate a template with the contents of a dictionary, your template will be passed a Context instance by default (not a RequestContext). To use a RequestContext in your template rendering, pass an optional third argument to render_to_response(): a RequestContext instance. Your code might look like this:

from django.template import RequestContext 
def getData(request):
  c = {}
  c.update(csrf(request))
  return render_to_response("app/index.html", c, context_instance=RequestContext(request))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top