views:

20

answers:

1

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

A: 

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 test it will get filled in. If you'd like to get a standalone test to work, add this to the top of your script:

from django.test.utils import setup_test_environment
setup_test_environment() 

Here's my whole test environment setup at the top of my script (for reference):

#!/usr/bin/env python

### Start ENV Setup
import os, sys
sys.path.append('/Users/me/Documents/Web/django_projects/myproject')

from django.core.management import setup_environ

import settings
print "Setting environment to:", setup_environ(settings), "\n"

from django.test.utils import setup_test_environment
setup_test_environment()
### Finish ENV Setup

#-------------------

# Start the Fun! >>
from myproject.myapp.models import mymodel
Scott Willman