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

Template Tag Routes to 'subdomain/page' #127

Closed starknetdev closed 3 years ago

starknetdev commented 3 years ago

I am having trouble with a button that routes from my main domain to subdomain. Development I have been doing on localhost, when manually entering the subdomain it works, however for UI would like the button to route users.

The button routes me to http://app/accounts/login/. I would like http://app.localhost:8000/accounts/login

host.py

from django.conf import settings
from django_hosts import patterns, host

host_patterns = patterns('',
    host('www', settings.ROOT_URLCONF, name='www'),  # <-- The `name` we used to in the `DEFAULT_HOST` setting
    host('app', 'app.urls', name='app'),
)

app.urls.py

from django.contrib import admin
from django.urls import path, include
from django.contrib.auth import views as auth_views

from . import views as app_views

urlpatterns = [
    path('', app_views.main_view, {'pagename': ''}, name='app_home'),
    path('accounts/login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
]

settings.py

MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',

    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',

    'django_hosts.middleware.HostsResponseMiddleware',
]

# Hosts
ROOT_HOSTCONF = 'testapp.hosts'  # `ebdjango` is the name of your project
DEFAULT_HOST = 'www'  # Name of the default host, we will create it in the next steps

base.html

<p><a href="{% host_url 'login' host 'app' %}" class="w3-button w3-black w3-padding-large w3-large w3-margin-top w3-hover-opacity-on">View Demo</a></p>

Any help would be greatly appreciated, have spent a while looking online and can't find anything.

Thanks, Sam

LowerDeez commented 3 years ago

Probably, you should add PARENT_HOST setting.

starknetdev commented 3 years ago

Thank you problem resolved!