Django子域名 [英] Django Subdomain

查看:121
本文介绍了Django子域名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个基本的商店应用程序。我设置了一个数据库,使每个产品都绑在一个特定的商店:我们打电话给商店鞋,玩具和书籍..我需要为应用程序设置子域名(这是在配置规格,没有选择)所以我可以映射到shoes.myapp.com,toys.myapp.com和books.myapp.com。我认为我需要做的是以某种方式设置子域名(我已经google了,但很困惑:是这样的方式?)然后,我想,从子域中的信息过滤我的数据库,以便只有具有商店的产品名称鞋例如出现在页面上。我在任何地方接近正确的轨道,还是有更好的方式来构建这个?

解决方案

我建议你使用这个应用程序: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆