How to access context from a custom django-admin command?

I have been using django.text client to examine the context of some URLs from several unit tests with a code similar to the following:

from django.test import TestCase

class MyTests(TestCase):

def example_test(self):
    
    response = self.client.get('/')
    # I can access response.context at this point and see all the key-value pairs that I use in my template file.

Now I am trying to do the same thing from a custom management command because I also need to do the same context-check-thing but using the prod database instead of the test database like in the previous example.
I am trying the following code without success:

from django.test import Client

class Command(BaseCommand):

def handle(self, *args, **kwargs):
    c = Client()
    response = c.get('/')
    # response.context is always None at this point

Also something like:

from django.template.context_processors import debug, request
from my_project_name.context_processor import my_custom_context_processor

template = Template(…)
processors = [debug, request, my_custom_context_processor]
my_request = RequestFactory().get(‘/’)
context = RequestContext(my_request, {}, processors)
# context is [{‘True’: True, ‘False’: False, ‘None’: None}, {}, {}, {}] at this point. What is not what I expected and is not the same result that the code in my first example (MyTest)
response = HttpResponse(template.render(context))
# At this point if I try to access response.context I get → AttributeError: ‘HttpResponse’ object has no attribute ‘context’

Can I access the context from my custom managed command like I do in the my MyTests class?

I don’t understand what your objective of doing this might be.

A custom management command isn’t run as the result of an http request and doesn’t return a response. Custom management commands are run by the command line. They don’t get a “request” object when they’re executed.

Thanks Ken to try to help with this.

I am aware that management commands are executed by command line and have nothing to do with requests and responses.

The main objetive of doing this is just iterate over several of the urls of my site and make some checks in the context before a release to production. Just as an additional verification that everything is working as expected.

Inside the code of my views I add dynamic key-value pairs to the context depending on several characteristics of the content, so I would like to be 100% sure that everything is ok before any code release to production.

So this is more a test than a custom management command. But I am using using custom management comand because I need to make that checks using the production database. I got the idea from here: python - How to run django unit-tests on production database? - Stack Overflow

Does this make sense to you?

In general principle, yes this makes a lot of sense. We run tests like this predeployment as well - except we’re testing the views as views. (We issue HttpRequests and inspect the HttpResponse.)

Fundamentally and architecturally, there’s a huge difference between the unit test environment and the actual Django execution environment. From The Django test client:

setup_test_environment() installs a template renderer which will allow us to examine some additional attributes on responses such as response.context that otherwise wouldn’t be available.

You might try calling this in your management command to see if it will work for you in your situation.

(Also be sure to review the docs at Advanced testing topics | Django documentation | Django)

1 Like

It worked!.
After calling it I can easily check the context in the response of a django.test Client.
Thanks a lot!