I'm trying to make a basic store app. I've set up a database so that every product is tied to a particular store: let's call the stores Shoes, Toys, and Books.. I need to set up subdomains for the app (it's in the assignment specs, no choice there) so that I can map to shoes.myapp.com, toys.myapp.com and books.myapp.com. What I think I need to do is somehow set up the subdomain (which I've googled but am confused about: is this the way to go?) and then, I guess, filter my databases from the info in the subdomain so that only products that have the store name "Shoes" for example appear on the page. Am I anywhere approaching the right track or is there a much better way to structure this?
Asked
Active
Viewed 6,969 times
7
-
what webserver are you running? You may want to use your webserver to translate the subdomain to an environment variable. – Mike Fogel Jan 24 '13 at 00:02
-
did your problem solve? – Alireza Sanaee Jul 03 '14 at 23:01
1 Answers
3
I suggest you to use this application: django-subdomains. http://django-subdomains.readthedocs.org/en/latest/index.html
And then, in your settings.py, you should use:
SUBDOMAIN_URLCONF = {
'toys': 'yourproject.urls.toys',
'shoes': 'yourproject.urls.shoes'
(...)
}
If you need to use the name of the subdomain in a view, it will be attached to the request object:
def your_view(request):
subdomain = request.subdomain
products = Products.objects.filter(store=subdomain) #an example how to use it to specif database queries. I dont know how your models are

silviojr
- 166
- 2
-
1OK--this looks like what I want... a little confused though. So, I think the second part with the request object makes sense to me, which is the important part. I've read the docs and your example a couple times, though, and I'm still a little lost. My URLs follow this structure: myproject.storename.pagename The store names are toys, shoes, books. The page names are 'products', 'checkout', and 'shoppingcart' along with / for the homepage (is that how you'd phrase that?). So 12 pages altogether--four for each of three stores. Do I need to map those all out individually? – thumbtackthief Jan 24 '13 at 00:59
-
1
-
2If the behavior is the same for all your apps, you can map the stores all together AND DEAL with each subdomain like in my example view. I don't understand why this is not pythonic. – silviojr Jan 24 '13 at 01:53
-
I meant what I said, not your answer. I shouldn't have to write out 12 urls, right? I'm very new here and not quite following your suggestion. – thumbtackthief Jan 24 '13 at 12:36
-
Can anyone elaborate the answer something like [Van Gale's answer](http://stackoverflow.com/questions/676457/how-do-i-set-urlpatterns-based-on-domain-name-or-tld-in-django) for the same thing without using the package – Ashish Gupta Dec 24 '14 at 04:16
-
Please notice that django-subdomains cannot be used with Django 1.9 or upper versions. But maybe you can check https://github.com/jazzband/django-hosts/ – cengineer Mar 21 '18 at 12:49