1

I am working on django project. It utilizes multiple small apps - where one of them is used for common things (common models, forms, etc).

I want to separate whole for project to two domains, i.g.:

corporatedomain.com and userportal.com

I want corporatedomain.com to use different urls, same for userportal.com.

Is that possible? If so, how can I do this? How should I configure my urls?

dease
  • 2,975
  • 13
  • 39
  • 75

2 Answers2

1

Maybe you can look at the Django Site Framework. From Django official documentation:

Django comes with an optional “sites” framework. It’s a hook for associating objects and functionality to particular Web sites, and it’s a holding place for the domain names and “verbose” names of your Django-powered sites.

You can use then this approach

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.contrib.sites.models import Site

current_site = Site.objects.get_current()

if 'userportal' in current_site.domain:
    urlpatterns = patterns('',
        url(r'', include('userapp.urls')),
        url(r'^admin/', include(admin.site.urls)),
    )
else:
    urlpatterns = patterns('',
        url(r'', include('corporateapp.urls')),
        url(r'^admin/', include(admin.site.urls)),
    )

You should add as many entries as you need to Site Table and add django.contrib.sites app in your INSTALLED_APP and also a SITE_ID variable to your settings bound with the correct site ID. Use SITE_ID = 1 when no domain info are available (for example in your development session). More info about SITE_ID in this post). In my settings I use the following approach:

SITE_ID = os.environ.get('SITE_ID', 1)

where I have set the right SITE_ID variable in each of my enrivorments.

Community
  • 1
  • 1
Salvatore Avanzo
  • 2,656
  • 1
  • 21
  • 30
0

You will have separate settings file anyway so define different ROOT_URLCONF for each domain.

UPDATE: If you don't want to use different settings then you have to write the middleware which will change the request.urlconf attribute using the HTTP_HOST header. Here is the example of such middleware.

catavaran
  • 44,703
  • 8
  • 98
  • 85
  • How can I do this? :) – dease Apr 01 '15 at 12:25
  • Do you have single settings file for both domains or use different settings for different domains? – catavaran Apr 01 '15 at 12:29
  • Currently I have single settings.py file – dease Apr 01 '15 at 12:33
  • Then it is time to have two :-) But if you really want to use single settings for both domains then read the update in my answer. – catavaran Apr 01 '15 at 12:46
  • Okay, If I want to use 2 settings files, where can I configure it? I mean, where I tell server to use particular settings for some domain? – dease Apr 01 '15 at 12:53
  • It depends on your deployment configuration. You can pass the `--settings` parameter to the `manage.py` command or use the `DJANGO_SETTINGS_MODULE` environment variable: https://docs.djangoproject.com/en/1.7/ref/django-admin/#django-admin-option---settings – catavaran Apr 01 '15 at 14:00
  • Also this page is worth to read: https://code.djangoproject.com/wiki/SplitSettings#SimplePackageOrganizationforEnvironments – catavaran Apr 01 '15 at 14:03