Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:11:03+00:00 2026-06-02T12:11:03+00:00

I have a django app that uses a custom middleware module to create a

  • 0

I have a django app that uses a custom middleware module to create a subdomain attribute in all requests. This attribute is assigned a string. Everything works during system testing, but I would like to run automated tests on this attribute so my question is:

When I generate a request during unit testing how do I set request.subdomain to a string value so I can test the code? Do I need to create a custom request and then feed it into the test client? Thanks for your time.

Solution Below

The custom middleware reads the HTTP_HOST string and saves the subdomain in the attribute request.subdomain. My problem was in how to squirt the subdomain request into the client during unit testing. Here’s how:

The test Client object allows you to preset any of the key:value pairs in the request.META dictionary. When running tests if you want to set the host name to a subdomain, do it like so:

host = 'subdomain1.test.com:8000' 
c = Client(HTTP_HOST=host)
response = c.get(path='/home')

And the test will execute as if someone typed subdomain1.test.com:8000/home in the browser.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:11:06+00:00Added an answer on June 2, 2026 at 12:11 pm

    All middlewares work normally during testing. So you can test the whole view (and check if it returns something specific for your request) – it’s functional testing. Or you can create a MockRequest and pass it directly to your middleware’s process_request – it is unit testing. Actually, I’d use both altogether.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django app that use a django-tagging. I need to port this
This is probably a setting error somewhere. I have a django app that works
I have a Django app that uses MySQL as a backend. I'm having difficulties
I have a django project that uses views from an external app. The view
I have a Django app that allows the user to create variables and name
I have a simple django app that is using only the admin. This is
I have a django app that uses MySQL as the database backend. It's been
If MyISAM doesn't have FK integrity, how does a django app that uses MyISAM
I have written a Django app that makes use of Python threading to create
I have a single model in my django app that I want to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.