subdomains and HOST in settings.py?

67 views
Skip to first unread message

Abraham Varricatt

unread,
Feb 5, 2015, 7:12:24 AM2/5/15
to django...@googlegroups.com
Hello,

I'm trying to implement custom subdomains on a customer site and am having trouble understanding how the HOST option in settings.py works. The requirement is that if a user logs in, they should see their own username as the subdomain. So for example if we have 3 users - mark, alice and bob. Now, if mark is logged in, he should interact with http://mark.example.com . If alice is logged in, she should access http://alice.example.com and so on. 

I've implemented this via custom middleware. What the middleware does is; if whoever is accessing the site is a logged in user, check if they are accessing the site via their respective subdomain entry. If not, prepend their username to their request and issue a redirect to their web-browser. The idea here is that if a logged in user (mark) tried to access http://www.example.com/food/apple then he would be redirected to http://mark.example.com/food/apple

This works half-way. My users are able to login to the site, but for some cases, where I need to call 3rd party APIs (and some jquery), things aren't working as they should. It's kindof difficult to describe the issue, but what I discovered is that in my settings.py file, if I set HOST=mark.example.com , then the site works perfectly for user mark, but is partly broken for the others. If I set HOST=alice.example.com, things work perfectly for alice, but not anyone else. 

Does anyone have any good explanation for this behavior? Or should I be trying a different approach? We're running django 1.5.

Puzzled,
Abraham V.



Abraham Varricatt

unread,
Feb 5, 2015, 7:48:26 AM2/5/15
to django...@googlegroups.com
Ok, I think I goofed up asking the question here. I noticed that there was a HOST setting defined in the django documentation and assumed it was the same thing, but some more code investigation, reveals otherwise. 

But I *am* running into a tangential issue. I mentioned that if I set the value of HOST=alice.example.com that the site works for alice? Well, I did a bit of middleware tweaking so that the value of settings.HOST would get changed depending on the user, but that doesn't seem to have helped. :(

-Abraham V.

Vijay Khemlani

unread,
Feb 5, 2015, 8:19:19 AM2/5/15
to django...@googlegroups.com
I'm not following, in the Django settings there is no "HOST" entry (other than the one used to connect to the database)


If you're talking about "ALLOWED_HOSTS", then that one is only a whitelist of allowed domains.

Try and describe your problem with more detail, What error are you getting? What problem with JQuery are you having?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52349660-aaa1-44ac-9c5f-d3b8fa28a230%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Guilherme Leal

unread,
Feb 5, 2015, 10:45:07 AM2/5/15
to django...@googlegroups.com
Basiacally, what you want to do is change the request subdomain based on the user that logs on to the app. 

Ex: the url that hosts the login form (or whatever it is) is like "http://exemple.com/login". After the user correctly logs in, every request will be made to de url "http://<username>.exemple.com".

Is that correct?

mdj

unread,
Feb 5, 2015, 10:45:38 AM2/5/15
to django...@googlegroups.com
If you're talking about ALLOWED_HOSTS, make sure you use something like ALLOWED_HOSTS = ['.example.com']. The dot is significant.

You will also need to set SESSION_COOKIE_DOMAIN = '.example.com' so cookies work across subdomains.

Abraham Varricatt

unread,
Feb 5, 2015, 10:58:27 AM2/5/15
to django...@googlegroups.com
@Guilherme Leal,

Yes, that's correct. Do you have any ideas?

-Abraham V.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/NIEINxJQnM0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Guilherme Leal

unread,
Feb 5, 2015, 11:07:23 AM2/5/15
to django...@googlegroups.com
I've made something similar. The app had multiple databases, and the database that was connected was based on the subdomain of the request. Basically, if you tried to log in with "http://test.exemple.com", the database that would be used would be "test".

The key to this feature was that the auth was on a separate db, and on that db existed a profile table that indicates on wich database every user would login.

I think that it's kinda the inverse from what you need.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Abraham Varricatt

unread,
Feb 5, 2015, 11:18:59 AM2/5/15
to django...@googlegroups.com
It's more like, if a user tried logging into http://www.example.com he would be redirected to http://<username>.example.com

Any simple ways to enforce this? Visually, the 2 links would be identical to the end-user (barring their login name somewhere)

-Abraham V.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/NIEINxJQnM0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Guilherme Leal

unread,
Feb 5, 2015, 11:22:05 AM2/5/15
to django...@googlegroups.com
Lets aproach this from other angle. What effect this would have on your app? Despite the fact that are different subdomains, there are other logic that the app implements based on the subdomain?

Reply all
Reply to author
Forward
0 new messages