django_hosts - NoReverseMatch: Reverse for 'home' not found. 'home' is not a valid view function or pattern name

1k views Asked by At
Versions:
Django                   2.2.10
django-hosts             4.0

I have installed django_hosts successfully as per this documentation.

I can successfully now access pages like https://blog.mysite.com

However, the urls on the page are NOT resolved correctly. I have followed the example shown in the django_hosts official documentation.

This is what my setup looks like:

mysite/urls.py

# imports etc. truncated for brevity 
# ...

urlpatterns = [
    path('', include(('home.urls', 'home'), namespace='home')),   
    path('blog/', include('blog.urls', namespace="blog")),
    # ...
]

home/urls.py

from django.urls import path
from django.conf.urls import include, url 

from .views import HomePageView, AboutView, TermsView, PrivacyView, \
                    sample, register, signin

app_name = 'home'

urlpatterns = [
    path('', HomePageView.as_view(), name='index'),
    path('about', AboutView.as_view(), name='about'),
    path('terms', TermsView.as_view(), name='terms'),
    path('privacy', PrivacyView.as_view(), name='privacy'),

    path('sample', sample),    
    path('register', register, name='register'),
    path('signin', signin, name='signin'),
]

blog/templates/index.html

                    <div class="container">
116                     <!-- Logo -->
117                     <a class="logo" href="{% host_url 'home' host 'www' %}" style="text-decoration: none; font-size: 250%;">
118                         <img src="/static/assets/img/logo.png">
119                         My Site
120                     </a>                    
121                     <!-- End Logo -->

[[ Edit ]]

The actual demo is uploaded to github

1

There are 1 answers

16
cizario On BEST ANSWER

below how i managed to solve the issue

. mysite

  .. blog  # Blog app
     .. __init__.py
     .. urls.py

  .. home  # Home app
     .. __init__.py
     .. urls.py


  .. mysite

     .. urls
        .. __init__.py
        .. blog.py
        .. home.py

     .. settings.py

     .. hosts.py

in mysite/settings.py

[..]

ALLOWED_HOSTS = [
              'mysite.com',
         'blog.mysite.com',
]

[..]


MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',  # django hosts

    [..]

    'django_hosts.middleware.HostsResponseMiddleware',  # django hosts
]

ROOT_URLCONF = 'mysite.urls.home'

# django-hosts
# https://django-hosts.readthedocs.io/en/latest/#settings

ROOT_HOSTCONF = 'mysite.hosts'

DEFAULT_HOST = 'www'  # HERE see hosts.py

PARENT_HOST = 'mysite.com'

# HOST_SCHEME = 'http'

# HOST_PORT = 8000

# HOST_SITE_TIMEOUT = 3600

[..]

in mysite/hosts.py

from django.conf import settings

from django_hosts import patterns, host


host_patterns = patterns('',

    # mysite.com
    host(r'^$', 'settings.ROOT_URLCONF', name='www'),  # see 'settings.py'

    # blog.mysite.com
    host(r'blog', 'mysite.urls.blog', name='blog'),
)

in mysite/urls/home.py

from django.conf import settings
from django.contrib import admin
from django.urls import path, include


urlpatterns = [
    path('',       include('home.urls', namespace='home')),

    path('admin/', admin.site.urls),  # Admin Area
]

[..]

in mysite/urls/blog.py

from django.urls import path, include


urlpatterns = [
    path('', include('apps.blog.urls', namespace='blog')),

]

[..]

in blog/templates/index.html

<div class="container">
    <!-- Logo -->
    
     <!-- HERE you don't need to specify host 'www' since it defaults to 'www' see settings.py -->
    <a class="logo" href="{% url 'home:index' %}" style="text-decoration: none; font-size: 250%;">
        <img src="/static/assets/img/logo.png">
        My Site
    </a>
    <!-- End Logo -->