如何在 Django 中根据域名或 TLD 设置 urlpatterns?
如何在 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ücher
或 Livres
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在网络服务器级别(例如在 Apache 中使用 mod_rewrite)或使用中间件(例如 此片段)另请参阅这个问题更新:在您发表评论后,我又想了一些。 我喜欢 Carl Meyer 的答案,但后来意识到它无法正确处理 {% url %} 反转。 所以这就是我要做的:
多个站点:您需要使用 Django 站点框架。 这意味着使用 Django 管理员为每种语言创建站点实例。
多种设置:每种语言网站也将有自己的settings.py。 每个站点之间的唯一区别在于
SITE_ID
和ROOT_URLCONF
设置,因此请遵循 DRY 原则,您应该将常用设置保留在不同的文件中,并将它们导入到主文件中,如下所示:...等等。
多个 URL 配置: 正如上面所暗示的,每个站点的 url 配置:
...等等。
这样,所有语言的 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 questionUpdate: 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
andROOT_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:... and so on.
Multiple URL conf: As implied above, a url conf for each site:
... 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 withSITE_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 :)
您可以使用中间件来完成此操作,该中间件通过 request.META['HTTP_HOST'] 检索 TLD 并将其添加到 request.path 之前; 那么您的根 URLconf 可以切换到基于 TLD 作为第一个 URL 路径段的特定于语言的 URLconf。 像这样的东西(未经测试!):
在你的 URLconf 中:
然后 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!):
And in your URLconf:
And then de_urls.py, fr_urls.py, and en_urls.py could each have all the URLs you need in the appropriate language.
在 django 中有一个名为“Sites”的表。 也许你可以用它做点什么?
In django there's a table called "Sites". Maybe you can do something with that?