[Django]-Change the default domain of Client() in unittest of Django

39👍

Django’s Client extends RequestFactory so you should be able to pass in extra params as keyword arguments.

Try:

response = self.c.get('/emails/html/upload', SERVER_NAME="mydomain.com")

1👍

The code can help not only in unit test, but it can also help for DRF to use context in a serializer
ResponseSerializer(instance=obj, context={'request': get_request}).data

from django.test.client import RequestFactory
rf = RequestFactory()
rf.defaults['SERVER_NAME'] = 'my-site.com'
get_request = rf.get('/hello/')

Leave a comment