3

I want to render two different HTML samples and send it back as response to ajax request.

I have something like this in my view:

def getClasses(request):
   User = request.user 
   aircomcode = request.POST.get('aircompany_choice', False)

   working_row = Pr_Aircompany.objects.get(user=User, aircomcode=aircomcode)
   economy_classes = working_row.economy_class
   business_classes = working_row.business_class

   economy = render_to_response('dbmanager/classes.html', {"classes": economy_classes}, content_type="text/html")
   business = render_to_response('dbmanager/classes.html', {"classes": business_classes}, content_type="text/html")

   return JsonResponse({"economy": economy, 
                    "business": business})

With this I get the error:

django.http.response.HttpResponse object at 0x7f501dc56588 is not JSON serializable"

How can I do my task?

In js when I get the response I would like to insert received HTML into corespoding blocks. Like this:

$.ajax({ # ajax-sending user's data to get user's classes
    url: url,
    type: 'post',
    data: {"aircompany_choice": aircompany_choice}, # send selected aircompanies for which to retrieving classes required
    headers: {"X-CSRFToken":csrftoken}, # prevent CSRF attack
}).done (result) ->
    add_booking_classes.find(".economy-classes").children(":nth-child(2)").html(result["economy"])
    add_booking_classes.find(".business-classes").children(":nth-child(2)").html(result["business"])
1
  • You shouldn't send html in JSON, it is a bad approach ... Commented Jul 5, 2015 at 19:27

3 Answers 3

6

Try Django's render_to_string :

economy = render_to_string('dbmanager/classes.html', {"classes": economy_classes})
business = render_to_string('dbmanager/classes.html', {"classes": business_classes})

render_to_string() loads a template, renders it and then returns the resulting string. You can then send these resulting strings as JSON.

Your final code now becomes:

from django.template.loader import render_to_string

def getClasses(request):
   User = request.user 
   aircomcode = request.POST.get('aircompany_choice', False)

   working_row = Pr_Aircompany.objects.get(user=User, aircomcode=aircomcode)
   economy_classes = working_row.economy_class
   business_classes = working_row.business_class

   economy = render_to_string('dbmanager/classes.html', {"classes": economy_classes})
   business = render_to_string('dbmanager/classes.html', {"classes": business_classes})

   return JsonResponse({"economy": economy, 
                    "business": business})
Sign up to request clarification or add additional context in comments.

Comments

2

render_to_response is, as the name implies, for rendering a response. You don't want to do that; you want to render two templates, and put them into a JSON response. So use render_to_string.

Comments

0

You can send one in your context and one as where you want to render.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.