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

  • SEARCH
  • Home
  • 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 6560801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:30:30+00:00 2026-05-25T13:30:30+00:00

I’m writing a web application in Django that is accessed from multiple domains to

  • 0

I’m writing a web application in Django that is accessed from multiple domains to the same IP address. The idea is that each domain the application is accessed from will receive unique branding.

So for example, if there were two domains, reseller.com and oem.com, and you went to oem.com, it would take you to to the same website as reseller.com, but with differently themed content (say, sent from /static/oem.com/{files} instead of /static/reseller.com/{files}).

Basically my idea has been to define a custom template tag, that receives the SERVER_NAME as an argument, which would return the location of the content.

Are there any alternatives, or simply easier options?

Edit: I should probably add that I’m using MongoDB for this project, and as such it’s more than likely Django’s ORM won’t be used for the project.

Edit again: More clarification; I’m using nginx.

  • 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-05-25T13:30:31+00:00Added an answer on May 25, 2026 at 1:30 pm

    Not sure how to do simple rewrite rule in nginx. Aside from a template tag (if you are only swapping out static content, then I think a template tag is the way to go), if the sites are gonna be different at all, template-wise, you can handle it by writing a custom template loader.

    This allows you to pick what templates you would like to use when you render your page. This method has a graceful way of failing if the loader fails to find a matching template for your specific domain. If it doesn’t find a match, it will fall back to your main templates directory. So you could have custom stuff for some domains, and more generic for others.

    But to make a decision what to serve based upon a request header, you’ll need to make the request available to the loader via _thread_locals, I do this in some middleware:

    #custom.middleware.py
    try:
        from threading import local
    except ImportError:
        from django.utils._threading_local import local
    
    _thread_locals = local()
    
    def get_current_request():
        return getattr(_thread_locals, 'request', None)
    
    class RequestMiddleware():
        def process_request(self, request):
            _thread_locals.request = request
    

    Next write a template loader (update the path to your middleware):

    #custom.loaders.py
    from os.path import join
    from django.conf import settings
    from django.template import TemplateDoesNotExist
    from path.to.middleware import get_current_request
    
    def load_template_source(template_name, template_dirs=None):
        request = get_current_request()
        host = request.get_host()
        path_to_template_dir = None
        for site in settings.SITE_TEMPLATE_FOLDERS:
            if site[0] == host:
                path_to_template_dir = site[1]
                break
    
        if path_to_template_dir:
            try:
                filepath = join(path_to_template_dir, template_name)
                file = open(filepath)
                try:
                    return (file.read(), filepath)
                finally:
                    file.close()
            except IOError:
                    pass
    
        raise TemplateDoesNotExist(template_name)
    

    and lastly update your settings file with three things 1) add the template loader (make sure its listed first) 2) add the middleware 3) and then add a new variable SITE_TEMPLATE_FOLDERS with a tuple of tuples containing domains and paths to template folders:

    #settings.py
    
    .....
    
    TEMPLATE_LOADERS = (
        'custom.loaders.load_template_source',
        'django.template.loaders.filesystem.load_template_source',
        'django.template.loaders.app_directories.load_template_source',
    )
    
    MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'domain.middleware.SessionMiddleware',
        'custom.middleware.RequestMiddleware',
    )
    
    SITE_TEMPLATE_FOLDERS = (
        ('mydomain.com', '/path/to/templates'),
        ('myotherdomain.com', '/path/to/other/templates')
    )
    ...
    

    Seems like a lot, but now you can easily add a new domain via your settings file.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a text area in my form which accepts all possible characters from

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.