4

In Mezzanine there are two different URLs that you could use depending what content you want to be displayed. Is there a way to check inside the urls.py if the active domain is on a subdomain or just the "regular" domain?

This is the two different styles:

url('^$', direct_to_template, {'template': 'index.html'}, name='home'),
url('^$', mezzanine.blog.views.blog_post_list, name='home'),

I haven't found a solid way for this yet. I want to display blog_post_list if I am on a subdomain (wildcard / e.g. sub.example.com) and index.html if I am on the "main" domain (e.g. example.com)

Droadpc
  • 79
  • 7

1 Answers1

8

No, the urls.py is loaded when your app boots up and only looks at the path. The current URL is part of the request objects, that the Middleware and the View receives.

However, you could use a view to delegate the request, based on the URL path.

url('^$', my_view, name='home')

And the view could use the current request object to delegate to one of the two subsequent views.

def my_view(request):
    if 'something' in request.META['HTTP_HOST']:
        return something_view(request)
    else:
        return another_view(request)

However, there is an app to do what you want to do, django-hosts. It allows to load different urls.py files based on the host.

from django_hosts import patterns, host

host_patterns = patterns('path.to',
    host(r'api', 'api.urls', name='api'),
    host(r'beta', 'beta.urls', name='beta'),
)
C14L
  • 12,153
  • 4
  • 39
  • 52