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

**URL:** <https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356>\
**Category:** Forms & APIs\
**Created:** [January 22, 2023, 9:12pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356 "2023-01-22T21:12:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfonso00MA](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/alfonso00ma/32/10094_2.png) [@Alfonso00MA](https://forum.djangoproject.com/u/Alfonso00MA)\
**Post date:** [January 22, 2023, 9:12pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/1 "2023-01-22T21:12:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 22, 2023, 9:26pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/2 "2023-01-22T21:26:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alfonso00MA](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/alfonso00ma/32/10094_2.png) [@Alfonso00MA](https://forum.djangoproject.com/u/Alfonso00MA)\
**Post date:** [January 22, 2023, 10:21pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/3 "2023-01-22T22:21:17Z")

</div>

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](https://stackoverflow.com/a/1646530/2132478)

Does this make sense to you?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 22, 2023, 10:57pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/4 "2023-01-22T22:57:57Z")

</div>

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](https://docs.djangoproject.com/en/4.1/intro/tutorial05/#the-django-test-client):

> [`setup_test_environment()`](https://docs.djangoproject.com/en/4.1/topics/testing/advanced/#django.test.utils.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](https://docs.djangoproject.com/en/4.1/topics/testing/advanced/#testing-utilities.))

---

<div class="post-metadata">

**Author:** ![Alfonso00MA](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/alfonso00ma/32/10094_2.png) [@Alfonso00MA](https://forum.djangoproject.com/u/Alfonso00MA)\
**Post date:** [January 22, 2023, 11:22pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/5 "2023-01-22T23:22:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mia/32/24939_2.png) [@mia](https://forum.djangoproject.com/u/mia)\
**Post date:** [March 6, 2026, 9:53pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/6 "2026-03-06T21:53:32Z")

</div>

I get from django\_client.get(admin\_single\_object\_url) the response object. And this not works response.context.keys() with error

{TypeError}TypeError(“unhashable type: ‘ContextDict’”)

Why it happens? I would like to get all possible keys, because I can’t get my field value by response.context[‘my\_field\_name’].

I want to get the value that was returned in the reality in one of fileds for test. And I don’t want to call ModelAdmin object directly or use beautifullsoup for that. Can I do it via .context? Or are any other good ways?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 6, 2026, 10:27pm UTC](https://forum.djangoproject.com/t/how-to-access-context-from-a-custom-django-admin-command/18356/7 "2026-03-06T22:27:36Z")

</div>

The current topic is a question that is already marked as solved. If you have a separate issue that you would like assistance with, please open up a new topic for it.

When you do so, please include the complete code of the test.
