dease
dease

Reputation: 3076

Django project on two domains - limiting access to urls/views

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?

Upvotes: 1

Views: 565

Answers (2)

Salvatore Avanzo
Salvatore Avanzo

Reputation: 2786

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.

Upvotes: 1

catavaran
catavaran

Reputation: 45585

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.

Upvotes: 0

Related Questions