Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Django app instance for multiple domains

I try to create an Django app which will work for multiple domains with single app instance.

For example:

  • there are three domains: group1.com, group2.com, group3.com
  • each domain has restricted content available after login
  • user1 is associated with group1.com and group2.com
  • when user1 log in to group1.com and try to enter group2.com, he will be automatically log in into the group2.com
  • there is URL, e.g. DOMAIN_NAME/posts/ which will show all content for user which is logged in from all domains which are associated with this user (in this case, for user1 there should be all "posts" from group1.com and group2.com)
  • when user1 enter group3.com, he's not logged in

I used Django Site framework to associate user with domains - content restriction for user in specific domains works fine.

Additionally, I used SESSION_COOKIE_DOMAIN parameter in settings.py for "share" cookie between domains and, unfortunately, it only works for subdomains. For example, after set:

SESSION_COOKIE_DOMAIN = '.group.com'

and after I wrote simple middleware, I'm able to meet the requirements that I wrote above but only for subdomains, like 'one.group.com', 'two.group.com', 'three.group.com'.

I was looking for solution for handle that, but I haven't found an answer for newest Django 3.x framework.

Is there any way to handle that like I explained?

like image 868
matulik Avatar asked Apr 08 '26 16:04

matulik


1 Answers

I think "django-hosts" package for you.

Firstly, you should give permission some domain.

ALLOWED_HOSTS = [example1.com,example2.com,example3.com]

And than, you can use django-hosts

from django_hosts import patterns, host
host_patterns = patterns('path.to',
    host(r'api', 'api.urls', name='api'),
    host(r'beta', 'beta.urls', name='beta'),
)

You can see more information -> https://github.com/jazzband/django-hosts

like image 114
Ali Yaman Avatar answered Apr 11 '26 05:04

Ali Yaman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!