在Django中如何设置基于域名或TLD的urlpattern? [英] How do I set urlpatterns based on domain name or TLD, in Django?
问题描述
如何在Django中设置基于域名或TLD的urlpattern?
对于某些链接,Amazon会根据其网站tld以本地语言显示url。 / p>
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 =>和书)
(链接不完整,只显示为样本。)
是否可以获得主机urls.py中的名称? (请求对象在urls.py中不可用)或者可能在中间件的process_request中,并在urls.py(如何...)中使用它。
任何备用建议如何这样吗
#----------伪代码----------
website_tld = get_host(request).split(。)[ - 1]
#.fr法语:图书:Livres
#.de德语:书:Bücher
如果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) ,
)
需要根据tld和更高版本的模板构建url模式,< a href ={%url books%}> {%transbooks%}< / a>
将HTML呈现为< a href =Bücher>Bücher< / a>
或< a href =Livres> Livres< / a>
您必须在网络服务器级别(例如在Apache中使用mod_rewrite)或中间件mple 此片段)
另请参阅这个SO问题
更新:在您的评论之后,我再考虑一下。我喜欢Carl Meyer的答案,但后来意识到它不会正确地处理{%url%}。所以这里是我会做的:
多个网站:您需要使用 Django网站框架。这意味着使用Django管理员为每种语言制作网站实例。
多个设置:每个语言网站也将有自己的settings.py。每个站点之间的唯一区别将是 SITE_ID
和 ROOT_URLCONF
设置,以便遵循 DRY 原则,您应该将常用设置保存在不同的文件中,并将其导入主文件,如下所示:
#settings_fr.py
/ pre>
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 conf:如上所述,每个站点的url conf:
#urls_fr.py
urlpatterns = patterns('',
url(r'^ Livres / $',books_view,name =books),
)
#urls_de.py
urlpatterns = ,
url(r'^Bücher/ $',books_view,name =books),
)
...等等。
这样,url名称(在本例中为books)就是相同的所有语言,因此
{%url books%}
将正确反转,域名将是Site对象的domain_name字段,具有SITE_ID
。
多个Web服务器实例:为了使每个SITE正常工作,每个站点都需要自己的服务器实例。对于apache + mod_wsgi,这意味着每个SITE的不同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>
希望这是清楚的:)
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/$' % 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>
解决方案
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
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:# 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/$', books_view, name="books"), ) # urls_de.py urlpatterns = patterns('', url(r'^Bücher/$', 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 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:
# 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 :)
这篇关于在Django中如何设置基于域名或TLD的urlpattern?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!