About RequestFactory in Django Tests

In Django tests when you use RequestFactory to create fake requests, you can pass extra META parameters to the get(), post(), etc. methods as additional keyword arguments.

For example to pass a custom user agent you would do this:

1
2
3
4
5
6
factory = RequestFactory()
request = factory.get(
    "/", 
    {"page": "1"}, 
    HTTP_USER_AGENT="Facebot",
)

Tips and Tricks Programming Testing Django 4.2 Django 3.2 Django 2.2