I’ve got a view that I’m trying to test with the Client object. Can I get to the variables I injected into the render_to_response of my view?
Example View:
def myView(request):
if request.method == "POST":
# do the search
return render_to_response('search.html',{'results':results},context_instance=RequestContext(request))
else:
return render_to_response('search.html',context_instance=RequestContext(request))
Test:
c = Client()
response = c.post('/school/search/', {'keyword':'beagles'})
# how do I get to the 'results'
EDIT:
From the Docs, I’m pretty certain I should be using:
response.context["results"]
…but response.context AND response.template both return None
Well, found my own answer. When you run a test on it’s own, that stuff doesn’t get filled in, but if you run it with
manage.py testit will get filled in. If you’d like to get a standalone test to work, add this to the top of your script:Here’s my whole test environment setup at the top of my script (for reference):