如何在 Django 中根据域名或 TLD 设置 urlpatterns? [英] How do I set urlpatterns based on domain name or TLD, in Django?
问题描述
如何在 Django 中根据域名或 TLD 设置 urlpatterns?
How do I set urlpatterns based on domain name or TLD, in Django?
对于某些链接,亚马逊会根据其网站 tld 以母语显示 url.
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.de/bücher-buch-literatur/ ( de : books => bücher )
http://www.amazon.fr/Nouveautés-paraître-Livres/ ( fr : books => Livres )
http://www.amazon.fr/Nouveautés-paraître-Livres/ ( fr : books => Livres )
http://www.amazon.co.jp/和书-ユーズドブッ-英语学习/ ( jp : books => 和书 )
http://www.amazon.co.jp/和書-ユーズドブッ-英語学習/ ( jp : books => 和書 )
(链接不完整,仅作为示例显示.)
( the links are incomplete and just show as samples. )
是否可以在 urls.py 中获取主机名?(请求对象在 urls.py 中不可用)或者可能在中间件的 process_request 中并在 urls.py 中使用它(如何???)
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/$' % lang_word,books_view, name="books"),
)
url 模式需要基于 tld 和后面的模板构建,<a href="{% url books %}" >{% trans "books" %}</a>
将 html 呈现为 <a href="Bücher">Bücher</a>
或 <a href="Livres">Livres</a>代码>
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>
推荐答案
您必须在网络服务器级别(例如在 Apache 中使用 mod_rewrite)或使用中间件(例如 这个片段)
另见这个所以问题
更新:在您发表评论后,我又考虑了一些.我喜欢 Carl Meyer 的回答,但后来意识到它无法正确处理 {% url %} 反转.所以这就是我要做的:
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:
多个站点:您需要使用 Django 站点框架.这意味着使用 Django 管理员为每种语言创建站点实例.
Multiple sites: You need to use the Django sites framework. Which means making site instances for each language using the Django admin.
多种设置:每个语言站点也有自己的 settings.py.每个站点之间的唯一区别是 SITE_ID
和 ROOT_URLCONF
设置,因此请遵循 DRY 原则,您应该将通用设置保存在不同的文件中,并将它们导入到主文件中,如下所示:
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 *
...等等.
Multiple URL conf: 如上所述,每个站点的 url conf:
Multiple URL conf: As implied above, a url conf for each site:
# urls_fr.py
urlpatterns = patterns('',
url(r'^Livres/$', books_view, name="books"),
)
# urls_de.py
urlpatterns = patterns('',
url(r'^Bücher/$', books_view, name="books"),
)
...等等.
这样,所有语言的 url 名称(在此示例中为books")都相同,因此 {% url books %}
将正确反向,域名将是 domain_name 字段具有 SITE_ID
的 Site 对象.
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
.
多个网络服务器实例:为了让每个站点正常工作,每个站点都需要自己的服务器实例.对于 apache + mod_wsgi,这意味着每个站点都有不同的 wsgi 应用程序,如下所示:
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()
... 等等,以及每个站点的匹配 apache 虚拟主机:
... and so on along with matching apache virtual host for each site:
<VirtualHost *:80>
ServerName mybooks.fr
WSGIScriptAlias / /path/to/site_fr.wsgi
...
</VirtualHost>
希望这很清楚:)
这篇关于如何在 Django 中根据域名或 TLD 设置 urlpatterns?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!