I'd like to be able to use the reverse url lookups in order to link to a pre-set domain e.g:
我希望能够使用反向URL查找以链接到预设域,例如:
in a template:
在模板中:
<a href="{% url 'admin_site' %}">Admin</a>
Where the page may sit at http://www.mydomain.com/home
and the admin site might be http://admin.mydomain.com
- or when in dev mode, it might be http://devadmin.localhost
页面可能位于http://www.mydomain.com/home,管理站点可能是http://admin.mydomain.com - 或者在开发模式下,它可能是http://devadmin.localhost
I can set the domain in settings using environment variables - but how might I get the URL framework to put that domain in the page template?
我可以使用环境变量在设置中设置域 - 但是我如何获得将该域放入页面模板的URL框架?
Two simple routes to achieve this:
实现这一目标的两条简单路线:
-
Just create a redirect view that might sit at somewhere like
/go/admin
which will just redirect to whatever domain I set up.只需创建一个可能位于/ go / admin之类的重定向视图,它只会重定向到我设置的任何域。
-
Include my domain in the template context and rewrite the href something like
<a href="{{ ADMIN_SITE }}">
在模板上下文中包含我的域,并重写href类似的内容
Both options would work. But both have drawbacks: first one involves and extra redirect step, second one doesn't use the same url
tag as other links.
两种选择都有效。但两者都有缺点:第一个涉及和额外的重定向步骤,第二个不使用与其他链接相同的url标记。
1 个解决方案
#1
2
I don't think you can/should directly add external urls to your urls.py
. That file is for URLs that must be resolved from the django server, so if the page is in another server... and, you want to make use of {% url %}
it must be through a redirect.
我不认为您可以/应该直接将外部网址添加到您的urls.py.该文件适用于必须从django服务器解析的URL,因此如果页面位于另一台服务器中......并且您希望使用{%url%},则必须通过重定向。
I would do this:
我会这样做:
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
# ...
url(r'^remote_admin/$', RedirectView.as_view(url='http://admin.mydomain.com'),
name='remote_admin'),
url(r'^dev_admin/$', RedirectView.as_view(url='http://devadmin.localhost'),
name='dev_admin'),
)
Then {% url %}
should work as usual:
那么{%url%}应该像往常一样工作:
{% url 'remote_admin' %}
#1
2
I don't think you can/should directly add external urls to your urls.py
. That file is for URLs that must be resolved from the django server, so if the page is in another server... and, you want to make use of {% url %}
it must be through a redirect.
我不认为您可以/应该直接将外部网址添加到您的urls.py.该文件适用于必须从django服务器解析的URL,因此如果页面位于另一台服务器中......并且您希望使用{%url%},则必须通过重定向。
I would do this:
我会这样做:
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
# ...
url(r'^remote_admin/$', RedirectView.as_view(url='http://admin.mydomain.com'),
name='remote_admin'),
url(r'^dev_admin/$', RedirectView.as_view(url='http://devadmin.localhost'),
name='dev_admin'),
)
Then {% url %}
should work as usual:
那么{%url%}应该像往常一样工作:
{% url 'remote_admin' %}