How to make Django url dispatcher use subdomain?
I have a vague idea on how to solve this, but really need a push :)
I have a Django app running with apache (mod_wsgi). Today urls look like this: http://site.com/category/A/product/B/
What I would like to do is this: http://A.site.com/product/B
This means that the url dispatcher some how needs to pick up the value found in the subdomai开发者_Go百科n and understand the context of this instead of only looking at the path. I see two approaches:
- Use .htaccess and rewrites so that a.site.com is a rewrite. Not sure if this does the trick since I don't fully understand what the django url dispatcher framework will see in that case?
- Understanding how the url dispatcher DO work I could write a filter that looks at valid sub domains and provides this in a rewritten format to the url dispatcher code.
Any hints or solutions are very much appreciated! Thanks.
Have you looked at django.contrib.sites
? I think a combination of that, setting SITE_ID
in your settings.py
, and having one WSGI file per "site" can take care of things.
EDIT: -v
set.
django.contrib.sites is meant to let you run multiple sites from the same Django project and database. It adds a table (django.contrib.sites.models.Site
) that has domain
and name
fields. From what I can tell, the name
can mean whatever you want it to, but it's usually the English name for the site. The domain
is what should show up in the host part of the URL.
SITE_ID
is set in settings.py
to the id
of the site being served. In the initial settings.py
file, it is set to 1 (with no comments). You can replace this with whatever code you need to set it to the right value.
The obvious thing to do would be to check an environment variable, and look up that in the name
or domain
field in the Site
table, but I'm not sure that will work from within the settings.py
file, since that file sets up the database connection parameters (circular dependency?). So you'll probably have to settle for something like:
SITE_ID = int(os.environ.get('SITE_ID', 1)
Then in your WSGI file, you do something like:
os.environ['SITE_ID'] = 2
and set that last number to the appropriate value. You'll need one WSGI file per site, or maybe there's a way to set SITE_ID from within the Apache setup. Which path to choose depends on the site setup in question.
The sites framework is most powerful where you use Site
as the target of a ForeignKey
or ManyToManyField
so that you can link your model instances (i.e. records) to specific sites.
Mikes solution is correct if you want to have multiple sites with same apps with different content (sites module) on multiple domains or subdomains, but it has a drawback that you need to be running multiple instances of the Django process.
A better solution for the main problem about multiple domains or subdomains is to use a simple middleware that handles incoming requests with the process_request()
function and changing the documented urlconf
attribute (link) of the request object to the URLconf you want to use.
More details and an example of the per-request or per-domain URL dispatcher can be found at: http://gw.tnode.com/0483-Django/
Try adding a wildcard subdomain: usually *
.
精彩评论