Django子域名 [英] Django Subdomain
问题描述
我建议你使用这个应用程序:django-subdomains。 http://django-subdomains.readthedocs.org/en/latest/index.html
然后,在您的settings.py中,您应该使用:
SUBDOMAIN_URLCONF = {
'toys':'yourproject.urls.toys',
'shoes':'yourproject.urls.shoes'
(...)
}
如果您需要在视图中使用子域的名称,附加到请求对象:
def your_view(request):
subdomain = request.subdomain
products = Products.objects.filter(store = subdomain)#an示例如何使用它来指定数据库查询。我不知道你的模型是如何
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?
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
这篇关于Django子域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!