0

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

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341
  • home is a namesapce not a url. where the href should go ? – Eric Martin Oct 22 '20 at 13:42
  • I don't see how you followed the linked documentation. There's virtually nothing alike. –  Oct 22 '20 at 13:46
  • @Melvyn first link shows how to install and setup `django_hosts` app. Official documentation link shows how to use `host_url` template tag. – Homunculus Reticulli Oct 22 '20 at 13:59
  • @EricMartin I don't understand your comment. Please clarify – Homunculus Reticulli Oct 22 '20 at 13:59
  • [This documentation](https://django-hosts.readthedocs.io/en/latest/) shows the use of `host` instead of `path` for urlpatterns and a different application structure. What @EricMartin means is that your call signature for [include](https://docs.djangoproject.com/en/3.1/ref/urls/#include) is incorrect. Your first member of the tuple should be an iterable of patterns not a module reference. –  Oct 22 '20 at 14:02

1 Answers1

2

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 -->

cizario
  • 3,995
  • 3
  • 13
  • 27
  • Thanks for your answer. I notice that you have an unsual directory structure: there is a `mysite/urls` folder. Is this intentional, or a mistake? Also, is this unusual structure required to make the `django_hosts` ap work correctly when routing URLs? – Homunculus Reticulli Oct 22 '20 at 16:01
  • yes it is intentional and not a mistake, it's simply a python package (`__init__.py`) to make the application modular and easy to extend and to maintain. – cizario Oct 22 '20 at 16:08
  • @HomunculusReticulli you may have a look at the source code of `djangoproject.com` https://github.com/django/djangoproject.com/tree/master/djangoproject and you'll get the idea – cizario Oct 22 '20 at 16:15
  • But isn't it standard (i.e. recommend) practise to have each application fully self contained and 'independent'? So for example app `foo` would have it urls.py accessible at `foo.urls'. In this case, the urls for each app is stored under the `urls` folder of the main project - this is a departure from recommended practise - or am I missing something here – Homunculus Reticulli Oct 22 '20 at 16:16
  • yes but i think you are confused between `app` level and `project/global` `urls.py` files. in `foo/urls.py` you define all routes/paths regarding `foo` app and then you make them available/recognizable **globally** in `project/global` `urls.py` next to `settings.py` file via `include` – cizario Oct 22 '20 at 16:30
  • yes, what you described in your last comment, is the standard (i.e. recommended way) to structure a django application (and that's how my project is structured). However, the answer you gave, has a different project layout - with apps having their urls in the `mysite/urls` folder. I would have to do a major refactor of my code to use this non-standard layout - that is why I was asking whether this non-standard project layout is the only way to get routing with `django-hosts` to work. Is that the case? – Homunculus Reticulli Oct 22 '20 at 16:36
  • for `django-hosts` it's often good practice to separate **urls confs** depending on **sub-domains** you need that's why the former **global** `urls.py` usually one is splitted – cizario Oct 22 '20 at 16:36
  • I see. That makes sense. I'll see if I can somehow get it to work with my existing project layout - If I don't succeed, I'll have no choice but to use the new project layout you suggested - even though that means a lot of refactoring at my end – Homunculus Reticulli Oct 22 '20 at 16:36
  • refer to to doc `host(r'(foo|bar)', 'path.to.urls', name='foo-or-bar'),` `path.to.urls` is the key – cizario Oct 22 '20 at 16:38
  • in your case just make difference between `app namespace` and `host name`, i usually choose the same name to avoid confusion – cizario Oct 22 '20 at 16:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223470/discussion-between-homunculus-reticulli-and-cizario). – Homunculus Reticulli Oct 22 '20 at 16:41
  • No. I have tried pretty much everything and the issue remains. The problem seems to be with reverse resolving url names when using `django_hosts`. I am currently looking at using another django app if need be, to implement subdomains. – Homunculus Reticulli Oct 24 '20 at 14:38
  • Problem is that if I have a base template with page headings (for example) that contain links to the homepage etc., the current issue I m having means that the page `http://localhost` works fine whereas `http://blog.localhost` does not, because I get an error like: `NoReverseMatch at / 'home' is not a registered namespace`. Even though the page header was displayed correctly at `http://localhost` – Homunculus Reticulli Oct 24 '20 at 14:54
  • @HomunculusReticulli did you setup `vhosts` ? – cizario Oct 24 '20 at 15:25
  • 1
    I have uploaded code for a demo that replicates the problem, to github, so you can see what I'm actually doing. See here: https://github.com/Pointer2VoidStar/subdomain-demo – Homunculus Reticulli Oct 24 '20 at 15:58
  • 1
    @HomunculusReticulli the issue should be now solved (i wasted much time with my very old machine) and it works on windows7 win 32, wampserver2.5, python 3.8.7, refer to this repo : https://github.com/cizario/subdomain-demo, i know i should fork it but ... – cizario Oct 24 '20 at 19:49