I'd like to be able to use the reverse url lookups in order to link to a pre-set domain e.g:
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
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?
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.
Include my domain in the template context and rewrite the href something like <a href="{{ ADMIN_SITE }}">
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.
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.
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 'remote_admin' %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With