1

I have a python function in the view which returns an object, now i want to call this function from another function which actually returns HTTPResponse. How can i return both the python object and AJAX response together? First method returning python object.

def Populate_Config_Resources():
    data = ResourceManager.objects.filter(verified = 1)
    return data

second method returning httpresponse.

def Update_ResourceManager(request):
    try:
        do something...

    except Exception,e:
        return HttpResponse(e)

    # here I call that function.
    config_data = Populate_Config_Resources()
    return HttpResponse('success')
2
  • This question is a bit confusing. You don't seem to be using a template anywhere - you need to pass your object to the template render function. Commented Feb 19, 2014 at 12:23
  • Roseman, i definitely use a template but i didn't include it in my post as my problem was to send the object which i was not able to do, once i have sent it successfully i will iterate over it in the template. nonetheless, thank you, i will post the code from my template next time i ask such question. :) Commented Feb 19, 2014 at 13:32

2 Answers 2

1

If you want an AJAX response for that return value, then use:

return HttpResponse(json.dumps(config_data), content_type="application/json")

Or if you are using a template:

template = loader.get_template('config.html')
context = RequestContext(request, {"config_data": config_data})
return HttpResponse(template.render(context))
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of content_type this should be mimetype. Otherwise you will get TypeError: __init__() got an unexpected keyword argument 'content_type' azure function
0

use the code given below

return HttpResponse(config_data)

and for response you can check status

2 Comments

thanks, i can access the data but it will be in the text form. I actually want an object in my template so that i can iterate over it to get the required values.
user3327831 you must send a JSON, XML, or similar representation of the object.

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.