`django-hosts` not routing subdomains correctly, despite correct configurations.

I am really getting to the end of my tether with django-hosts

I want to redirect cerulix.com/blogblog.cerulix.com. But apparently that’s way too much of an ask.

Here’s my MRE:

I install the django-hosts module in my .venv by running the following command:

pip install django-hosts

#settings.py

MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',
    'django_hosts.middleware.HostsResponseMiddleware',
]

INSTALLED_APPS = [
    'blog',
    'django_hosts'
]

ALLOWED_HOSTS = [
    '127.0.0.1', 
    'cerulix.com', 
    'www.cerulix.com',
    'blog.cerulix.com',
]

ROOT_HOSTCONF = "cerulix.hosts"
DEFAULT_HOST = "www"
PARENT_HOST = 'cerulix.com'
#cerulix.hosts

from django_hosts import patterns, host
from django.conf import settings
from django.urls import path, include

host_patterns = patterns('',
                         
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'blog', 'blog.urls', name='blog')

)
#cerulix.urls

from . import views
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve

urlpatterns = [
    path('', views.index),
    path('blog', include('blog.urls'), name='blog'),

]
#blog.urls
from django.urls import path
from. import views

urlpatterns = [

    path('', views.blog, name='blog'),
    path('<slug:slug>', views.blog__details),

]
#index.html
{% load hosts %}

<a href="{% host_url 'blog' host 'blog' %}" >Blog</a></li>

As you can see, everything is set up correctly.

Guess what?

  • You type blog.<domain>.com into the URL, → index.html
  • You go to the blog <a href="> link, it does go to the blog, but the URL in the address bar is blog.cerulix.com/blog. How absolutely stupid can this thing be?
  • You then click off of the blog to go to another page, guess what, the address in the URL bar is - yup, you guessed it -blog.cerulix.com

So why? My configurations are correct, so why?

You have zero idea how sick to the back teeth I am of this.

What do you mean by “convert”? Do you mean redirect? If so, that doesn’t appear to be what django -hosts is for.

It allows you to route requests for different hosts to different urlconf files. That’s it.

It’s doing what you’ve told it:

You’re generating a link to the “blog” page on the host “blog”. So blog.cerulix.com/blog

Are the links you’re clicking generated using the host_url tag? If so, which host are they generating links for?

Yup, by “convert" I mean, redirect. I will reword.

It allows you to route requests for different hosts to different urlconf files. That’s it.

That’s exactly what django-hosts are used for.

It says in the official documentation, & it’s not doing what I have told it to do, I have told it go to the blog section & display blog.cerulix.com in the URL bar in the browser, just like every other website in the world that uses subdomains.

The other links I am clicking on AFTER I have visited the blog page, which django clearly seems to have an identity crisis over, are not hosted by the host_url tag - because they are hosted by the urls tag. It’s how it worked before django-hosts because the insanity of my life and now the browser is having a hissy fit over it.

Is this thing a django module or a baby?

Just every little thing, there’s an issue with it. Issue after error after issue after error after issue. I am so sick and tired of it. Why can’t something just do what I tell it to without arguing with me? Why do I have to waste my time, scream, rip my hair out just because this module decides when it is going to do as it’d told?

Then django-hosts is not what you need. It doesn’t redirect a request for one URL to another URL. It routes requests to urlconf files (and so URLs) based on the domain name. Without redirecting the browser to a new URL.

It’s doing exactly what you have told it to do.

django-hosts does not change what is in the URL bar in the browser. It looks at what is in the URL bar and serves a view depending on which domain is in that URL bar. That’s it.

So, if you look at the source of the page in your browser, you’ll see that those links are like /blog/foo/. i.e. they do not have https://<domain> at the start. Therefore they will go to that URL on the same domain as the current page. If you want them to go to a different domain, you’ll have to use the host_url tag.

You need to take a step back, understand how browsers, URLs and redirections work, understand the documentation for the tools you’re using, and quit blaming everything else for things not working when you haven’t set them up properly, or are using the wrong tools.

You’ve “told it” to do something it wasn’t designed to do.