如何让 Django url 调度程序使用子域?

发布于 2024-09-07 09:05:28 字数 608 浏览 1 评论 0原文

我对如何解决这个问题有一个模糊的想法,但确实需要推动:)

我有一个使用 apache (mod_wsgi) 运行的 Django 应用程序。今天的网址如下所示: http://site.com/category/A/product/B/

什么我想做的是这样的: http://A.site.com/product/B

这意味着 url 调度程序某些how 需要获取在子域中找到的值并了解其上下文,而不是仅查看路径。我看到两种方法:

  • 使用 .htaccess 并重写,以便 a.site.com 被重写。不确定这是否有效,因为我不完全理解 django url 调度程序框架在这种情况下会看到什么?
  • 了解 url 调度程序如何工作后,我可以编写一个过滤器来查看有效的子域,并以重写的格式将其提供给 url 调度程序代码。

非常感谢任何提示或解决方案! 谢谢。

I have a vague idea on how to solve this, but really need a push :)

I have a Django app running with apache (mod_wsgi). Today urls look like this:
http://site.com/category/A/product/B/

What I would like to do is this:
http://A.site.com/product/B

This means that the url dispatcher some how needs to pick up the value found in the subdomain and understand the context of this instead of only looking at the path. I see two approaches:

  • Use .htaccess and rewrites so that a.site.com is a rewrite. Not sure if this does the trick since I don't fully understand what the django url dispatcher framework will see in that case?
  • Understanding how the url dispatcher DO work I could write a filter that looks at valid sub domains and provides this in a rewritten format to the url dispatcher code.

Any hints or solutions are very much appreciated! Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔一刀 2024-09-14 09:05:29

如果您希望在多个域或子域上拥有具有不同内容(站点模块)的相同应用程序的多个站点,则 Mikes 的解决方案是正确的,但它有一个缺点,即您需要运行 Django 进程的多个实例。

对于多个域或子域的主要问题,一个更好的解决方案是使用一个简单的中间件,通过 process_request() 函数处理传入请求并更改记录的 urlconf 属性(<请求的 href="http://docs.djangoproject.com/en/dev/topics/http/urls/#how-django-processes-a-request" rel="nofollow">链接)对象您要使用的 URLconf。

每个请求或每个域 URL 调度程序的更多详细信息和示例< /a> 可以在以下位置找到:
http://gw.tnode.com/0483-Django/

Mikes solution is correct if you want to have multiple sites with same apps with different content (sites module) on multiple domains or subdomains, but it has a drawback that you need to be running multiple instances of the Django process.

A better solution for the main problem about multiple domains or subdomains is to use a simple middleware that handles incoming requests with the process_request() function and changing the documented urlconf attribute (link) of the request object to the URLconf you want to use.

More details and an example of the per-request or per-domain URL dispatcher can be found at:
http://gw.tnode.com/0483-Django/

2024-09-14 09:05:29

尝试添加通配符子域:通常是*

Try adding a wildcard subdomain: usually *.

风启觞 2024-09-14 09:05:28

你看过django.contrib.sites吗?我认为将其结合起来,在 settings.py 中设置 SITE_ID 并为每个“站点”创建一个 WSGI 文件就可以解决问题。

编辑: -v 设置。

django.contrib.sites 旨在让您运行多个站点来自相同的 Django 项目和数据库。它添加了一个包含 domainname 字段的表 (django.contrib.sites.models.Site)。据我所知,名称可以表示任何你想要的意思,但它通常是网站的英文名称。 domain 应该显示在 URL 的主机部分中。

SITE_IDsettings.py 中设置为所服务站点的 id。在初始 settings.py 文件中,它设置为 1(没有注释)。您可以将其替换为您需要的任何代码,以将其设置为正确的值。

显而易见的做法是检查环境变量,并在 Site 表中的 namedomain 字段中查找该变量,但是我不确定这是否可以在 settings.py 文件中工作,因为该文件设置数据库连接参数(循环依赖?)。因此,您可能必须满足以下要求:

SITE_ID = int(os.environ.get('SITE_ID', 1)

然后在 WSGI 文件中,您执行以下操作:

os.environ['SITE_ID'] = 2

并将最后一个数字设置为适当的值。每个站点都需要一个 WSGI 文件,或者也许有一种方法可以在 Apache 设置中设置 SITE_ID。选择哪条路径取决于相关站点设置。

站点框架最强大,您可以使用 Site 作为 ForeignKeyManyToManyField 的目标,以便您可以链接模型实例(即记录)到特定站点。

Have you looked at django.contrib.sites? I think a combination of that, setting SITE_ID in your settings.py, and having one WSGI file per "site" can take care of things.

EDIT: -v set.

django.contrib.sites is meant to let you run multiple sites from the same Django project and database. It adds a table (django.contrib.sites.models.Site) that has domain and name fields. From what I can tell, the name can mean whatever you want it to, but it's usually the English name for the site. The domain is what should show up in the host part of the URL.

SITE_ID is set in settings.py to the id of the site being served. In the initial settings.py file, it is set to 1 (with no comments). You can replace this with whatever code you need to set it to the right value.

The obvious thing to do would be to check an environment variable, and look up that in the name or domain field in the Site table, but I'm not sure that will work from within the settings.py file, since that file sets up the database connection parameters (circular dependency?). So you'll probably have to settle for something like:

SITE_ID = int(os.environ.get('SITE_ID', 1)

Then in your WSGI file, you do something like:

os.environ['SITE_ID'] = 2

and set that last number to the appropriate value. You'll need one WSGI file per site, or maybe there's a way to set SITE_ID from within the Apache setup. Which path to choose depends on the site setup in question.

The sites framework is most powerful where you use Site as the target of a ForeignKey or ManyToManyField so that you can link your model instances (i.e. records) to specific sites.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文