jazzband / django-hosts

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

django_hosts.resolvers.reverse NoReverseMatch with wildcard regex #45

Closed vitorfs closed 9 years ago

vitorfs commented 9 years ago

Not sure if I'm missing something, but I'm having trouble using reverse on wildcard subdomains.

hosts.py

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

host_patterns = patterns('',
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'(\w+)', app.custom_urls', name='wildcard'),
)

app.custom_urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import include, url

from app import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^articles/', include('app.articles.urls', namespace='articles')),
    url(r'^events/', include('app.events.urls', namespace='events')),
    url(r'^partners/', include('app.partners.urls', namespace='partners')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Now if I try something like:

from django_hosts.resolvers import reverse

reverse('home', host='wildcard')

I'm getting the error:

NoReverseMatch                            Traceback (most recent call last)
<ipython-input-2-e86d09f05052> in <module>()
----> 1 reverse('home', host='wildcard')

/Users/vitorfs/Development/venv/lib/python2.7/site-packages/django_hosts/resolvers.pyc in reverse(viewname, args, kwargs, prefix, current_app, host, host_args, host_kwargs, scheme, port)
    167     hostname = reverse_host(host,
    168                             args=host_args,
--> 169                             kwargs=host_kwargs)
    170     path = reverse_path(viewname,
    171                         urlconf=host.urlconf,

/Users/vitorfs/Development/venv/lib/python2.7/site-packages/django_hosts/resolvers.pyc in reverse_host(host, args, kwargs)
    119     raise NoReverseMatch("Reverse host for '%s' with arguments '%s' "
    120                          "and keyword arguments '%s' not found." %
--> 121                          (host.name, args, kwargs))
    122 
    123 #: The lazy version of the :func:`~django_hosts.resolvers.reverse_host`

NoReverseMatch: Reverse host for 'wildcard' with arguments '()' and keyword arguments '{}' not found.

Now if I change my hosts.py

hosts.py (removed regex)

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

host_patterns = patterns('',
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'test', app.custom_urls', name='wildcard'),
)

I can run:

In [1]: from django_hosts.resolvers import reverse

In [2]: reverse('home', host='wildcard')
Out[2]: '//test.testserver.local/'

Which is correct.

vitorfs commented 9 years ago

I just realised the right way to do it is:

In [2]: reverse('home', host='wildcard', host_args=('test',))
Out[2]: '//test.testserver.local/'