jazzband / django-hosts

Dynamic and static host resolving for Django. Maps hostnames to URLconfs.
http://django-hosts.rtfd.org
Other
979 stars 107 forks source link

URL Templates wrong redirect on localhost #150

Closed ogiel closed 2 years ago

ogiel commented 2 years ago

Hey all, I am having trouble with getting URL templates to work on localhost as well as on the actual domain. They work fine on the actual domain but when I run it on localhost it keeps redirecting to / instead of .localhost:8000. Is there a way to fix it so the URLs route to localhost in development and to the actual domain in production?

Files: settings.py

ROOT_URLCONF = '<project>.urls'
ROOT_HOSTCONF = '<project>.hosts'
DEFAULT_HOST = 'www'

hosts.py

host_patterns = patterns('',
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'ar-demo', 'ar_demo.urls', name='ar-demo'),
)

ar-demo urls.py

urlpatterns = [
    path('', views.ar_demo_page, name='ar-demo-page'),
]

index.html

{% load hosts %}
href="{% host_url 'ar-demo-page' host 'ar-demo' %}"

Now, as said above, the href redirects to ar-demo..com on the actual domain. But it redirects to http://ar-demo/ when it runs on localhost. The expected behavior is a redirect to ar-demo.localhost:8000.

Is there a way to fix the URL templates so it works both scenarios like the normal Django URL templates?

ogiel commented 2 years ago

Just found it; Adding PARENT_HOST = 'localhost:8000' fixes it local, it should be the domain name on production. I will close the issue but not remove it so others might refer to it later.