如何在 Django 中根据域名或 TLD 设置 urlpatterns?

发布于 2024-07-15 20:07:19 字数 1485 浏览 7 评论 0原文

如何在 Django 中根据域名或 TLD 设置 urlpatterns?

对于某些链接,亚马逊根据其网站顶级域名以母语显示网址。

http://www.amazon.de/bücher-buch-literatur/< /a> ( de : books => bücher )

http ://www.amazon.fr/Nouveautés-paraître-Livres/ ( fr : books => Livres )

http://www.amazon.co.jp/和书-ユーズドブッ-英语学习/ ( jp : books => 和书 )

(链接不完整,仅显示为示例。)

是否可以在 urls.py 中获取主机名? (请求对象在 urls.py 中不可用)或者可能在中间件的 process_request 中并在 urls.py 中使用它(如何???)

有任何替代建议如何实现此目的?

#---------- pseudocode ---------- 

website_tld = get_host(request).split(".")[-1]

#.fr French  : Books : Livres
#.de German : Books : Bücher

if website_tld == "fr":
    lang_word = "Livres"
elif website_tld == "de":
    lang_word = "Bücher"
else:
    lang_word = "books"

urlpatterns = patterns('',
                       url(r'^%s/$' % lang_word,books_view, name="books"),
                       )

url 模式需要基于 tld 构建,并稍后在模板中构建,{% trans "books" %} 将 html 渲染为 BücherLivres

How do I set urlpatterns based on domain name or TLD, in Django?

For some links, Amazon shows url in native language based on its website tld.

http://www.amazon.de/bücher-buch-literatur/ ( de : books => bücher )

http://www.amazon.fr/Nouveautés-paraître-Livres/ ( fr : books => Livres )

http://www.amazon.co.jp/和書-ユーズドブッ-英語学習/ ( jp : books => 和書 )

( the links are incomplete and just show as samples. )

Is it possible to get host name in urls.py? (request object is not available in urls.py) or maybe in process_request of middleware and use it in urls.py(how???)

Any alternate suggestions how to achive this?

#---------- pseudocode ---------- 

website_tld = get_host(request).split(".")[-1]

#.fr French  : Books : Livres
#.de German : Books : Bücher

if website_tld == "fr":
    lang_word = "Livres"
elif website_tld == "de":
    lang_word = "Bücher"
else:
    lang_word = "books"

urlpatterns = patterns('',
                       url(r'^%s/

The url pattern needs to be built based on tld and later in the template, <a href="{% url books %}" >{% trans "books" %}</a> to render html as <a href="Bücher">Bücher</a> or <a href="Livres">Livres</a>

% lang_word,books_view, name="books"), )

The url pattern needs to be built based on tld and later in the template, <a href="{% url books %}" >{% trans "books" %}</a> to render html as <a href="Bücher">Bücher</a> or <a href="Livres">Livres</a>

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

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

发布评论

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

评论(3

疯了 2024-07-22 20:07:19

您必须在网络服务器级别(例如在 Apache 中使用 mod_rewrite)或使用中间件(例如 此片段

另请参阅这个问题


更新:在您发表评论后,我又想了一些。 我喜欢 Carl Meyer 的答案,但后来意识到它无法正确处理 {% url %} 反转。 所以这就是我要做的:

多个站点:您需要使用 Django 站点框架。 这意味着使用 Django 管理员为每种语言创建站点实例。

多种设置:每种语言网站也将有自己的settings.py。 每个站点之间的唯一区别在于 SITE_IDROOT_URLCONF 设置,因此请遵循 DRY 原则,您应该将常用设置保留在不同的文件中,并将它们导入到主文件中,如下所示:

# settings_fr.py
SITE_ID = 1
ROOT_URLCONF = 'app.urls_fr'
from settings_common import *

# settings_de.py
SITE_ID = 2
ROOT_URLCONF = 'app.urls_de'
from settings_common import *

...等等。

多个 URL 配置: 正如上面所暗示的,每个站点的 url 配置:

# urls_fr.py
urlpatterns = patterns('',
    url(r'^Livres/

...等等。

这样,所有语言的 url 名称(在本例中为“books”)都是相同的,因此 {% url books %} 将正确反转,并且域名将是网站的 domain_name 字段具有 SITE_ID 的对象。

多个 Web 服务器实例:为了使每个站点正常工作,它们各自需要自己的服务器实例。 对于 apache + mod_wsgi 这意味着每个站点都有一个不同的 wsgi 应用程序,如下所示:

# site_fr.wsgi
import os, sys, django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings_fr'
application = django.core.handlers.wsgi.WSGIHandler()

...等等以及每个站点的匹配 apache 虚拟主机:

<VirtualHost *:80>
    ServerName mybooks.fr
    WSGIScriptAlias / /path/to/site_fr.wsgi
    ...
</VirtualHost>

希望这是清楚的:)

, books_view, name="books"), ) # urls_de.py urlpatterns = patterns('', url(r'^Bücher/

...等等。

这样,所有语言的 url 名称(在本例中为“books”)都是相同的,因此 {% url books %} 将正确反转,并且域名将是网站的 domain_name 字段具有 SITE_ID 的对象。

多个 Web 服务器实例:为了使每个站点正常工作,它们各自需要自己的服务器实例。 对于 apache + mod_wsgi 这意味着每个站点都有一个不同的 wsgi 应用程序,如下所示:


...等等以及每个站点的匹配 apache 虚拟主机:


希望这是清楚的:)

, books_view, name="books"), )

...等等。

这样,所有语言的 url 名称(在本例中为“books”)都是相同的,因此 {% url books %} 将正确反转,并且域名将是网站的 domain_name 字段具有 SITE_ID 的对象。

多个 Web 服务器实例:为了使每个站点正常工作,它们各自需要自己的服务器实例。 对于 apache + mod_wsgi 这意味着每个站点都有一个不同的 wsgi 应用程序,如下所示:

...等等以及每个站点的匹配 apache 虚拟主机:

希望这是清楚的:)

You have to do this at the webserver level (for example using mod_rewrite in Apache) or with middleware (for example this snippet)

Also see this SO question


Update: after your comment I thought about it some more. I liked Carl Meyer's answer, but then realized it wouldn't handle {% url %} reversing properly. So here's what I would do:

Multiple sites: You need to use the Django sites framework. Which means making site instances for each language using the Django admin.

Multiple settings: Each language site will also have its own settings.py. The only differences between each site will be the SITE_ID and ROOT_URLCONF settings so, to follow DRY principle, you should keep the common settings in a different file and import them into the master file like this:

# settings_fr.py
SITE_ID = 1
ROOT_URLCONF = 'app.urls_fr'
from settings_common import *

# settings_de.py
SITE_ID = 2
ROOT_URLCONF = 'app.urls_de'
from settings_common import *

... and so on.

Multiple URL conf: As implied above, a url conf for each site:

# urls_fr.py
urlpatterns = patterns('',
    url(r'^Livres/

... and so on.

This way the url name (in this example "books") is the same for all languages, and therefore {% url books %} will reverse properly and the domain name will be the domain_name field of the Site object with SITE_ID.

Multiple web server instances: In order for each SITE to work properly they each need their own server instances. For apache + mod_wsgi this means a different wsgi application for each SITE like this:

# site_fr.wsgi
import os, sys, django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'app.settings_fr'
application = django.core.handlers.wsgi.WSGIHandler()

... and so on along with matching apache virtual host for each site:

<VirtualHost *:80>
    ServerName mybooks.fr
    WSGIScriptAlias / /path/to/site_fr.wsgi
    ...
</VirtualHost>

Hopefully this is clear :)

, books_view, name="books"), ) # urls_de.py urlpatterns = patterns('', url(r'^Bücher/

... and so on.

This way the url name (in this example "books") is the same for all languages, and therefore {% url books %} will reverse properly and the domain name will be the domain_name field of the Site object with SITE_ID.

Multiple web server instances: In order for each SITE to work properly they each need their own server instances. For apache + mod_wsgi this means a different wsgi application for each SITE like this:


... and so on along with matching apache virtual host for each site:


Hopefully this is clear :)

, books_view, name="books"), )

... and so on.

This way the url name (in this example "books") is the same for all languages, and therefore {% url books %} will reverse properly and the domain name will be the domain_name field of the Site object with SITE_ID.

Multiple web server instances: In order for each SITE to work properly they each need their own server instances. For apache + mod_wsgi this means a different wsgi application for each SITE like this:

... and so on along with matching apache virtual host for each site:

Hopefully this is clear :)

野侃 2024-07-22 20:07:19

您可以使用中间件来完成此操作,该中间件通过 request.META['HTTP_HOST'] 检索 TLD 并将其添加到 request.path 之前; 那么您的根 URLconf 可以切换到基于 TLD 作为第一个 URL 路径段的特定于语言的 URLconf。 像这样的东西(未经测试!):

class PrependTLDMiddleware:
""" Prepend the top level domain to the URL path so it can be switched on in 
a URLconf. """

def process_request(self, request):
    tld = request.META['HTTP_HOST'].split('.')[-1]
    request.path = "/%s%s" % (tld, request.path)

在你的 URLconf 中:

urlpatterns = patterns('',
    url(r'^de/' include('de_urls')),
    url(r'^fr/', include('fr_urls')),
    url(r'^[^/]+/', include('en_urls'))
)

然后 de_urls.py、fr_urls.py 和 en_urls.py 都可以以适当的语言包含你需要的所有 URL。

You can probably do this with a middleware that retrieves the TLD via request.META['HTTP_HOST'] and prepends it to request.path; then your root URLconf can switch out to language-specific URLconfs based on the TLD as the first URL path segment. Something like this (untested!):

class PrependTLDMiddleware:
""" Prepend the top level domain to the URL path so it can be switched on in 
a URLconf. """

def process_request(self, request):
    tld = request.META['HTTP_HOST'].split('.')[-1]
    request.path = "/%s%s" % (tld, request.path)

And in your URLconf:

urlpatterns = patterns('',
    url(r'^de/' include('de_urls')),
    url(r'^fr/', include('fr_urls')),
    url(r'^[^/]+/', include('en_urls'))
)

And then de_urls.py, fr_urls.py, and en_urls.py could each have all the URLs you need in the appropriate language.

熟人话多 2024-07-22 20:07:19

在 django 中有一个名为“Sites”的表。 也许你可以用它做点什么?

In django there's a table called "Sites". Maybe you can do something with that?

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