dease
dease

Reputation: 3076

Dynamic subdomains and url paths in django project

I am trying to implement a dynamic subdomain and urls system in my django project.

Generally, each user has its domain, eg. myusername.example.com User may define dynamic URLs, for example:

But I have also my website running on example.com, with urls like example.com/contact, example.com/about-us and so on.

I want to all of these URLs to point to my custom view (class based) where I do some DB queries and return dynamic content. this somethin1/something2 part is fully dynamic, and there may be anything . defined by user.

I've got something like this:

urls.py

from web.views import HomeView, ContactView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('contact', ContactView.as_view()),
    path('', HomeView.as_view()),
    re_path('.*', HomeView.as_view())
]

web.views.py

class HomeView(TemplateView):
    template_name = 'home.html'

    def dispatch(self, request, *args, **kwargs):

        SERVICE_DOMAIN = settings.DOMAIN

        http_host = str(request.META.get('HTTP_HOST'))

        if SERVICE_DOMAIN in http_host:
            subdomains = http_host.split(SERVICE_DOMAIN)[0]
            subdomain = slugify.slugify(subdomains)
        else:
            subdomain = False

        if subdomain:
            print('Our subdomain is {}'.format(subdomain))
            kwargs['subdomain'] = subdomain
            return CustomUrlView.as_view()(self.request, *args, **kwargs)

        if not subdomain:
            print('run normally')
            return super().dispatch(request, *args, **kwargs)

class CustomUrlView(View):
    def dispatch(self, request, *args, **kwargs):
        subdomain = kwargs.get('subdomain')
        url = request.META.get('PATH_INFO').lower().strip().replace('/', '', 1)
        # here I do some queries in DB with my url variable - it has own model etc.

Generally, everything works fine for almost all user defined urls... Problem is when visitor opens myusername.example.com/contact - its always match the url defined in urls.py and and its not catched by my HomeView.

How can I solve it? I don't want to use any of my class based view's urls defined in urls.py when request comes from a subdomain.

Upvotes: 3

Views: 2796

Answers (1)

Goran
Goran

Reputation: 6834

You need to have a two different urls files. One for domain and second for subdomains.

  • Split domain and subdomain views in two url files. If you have views which works on both e.g. login, create a "common" file and include in both urls.
  • You can choose which url you will use, so create a middleware and inspect a host request.META.get('HTTP_HOST'). If request comes from subdomain, then simply load appropriated urls request.urlconf = 'path.to_subdomain.urls'

Note: Be sure that ROOT_URLCONF in your settings.py point to the "domain' urls. Also, in your middleware you should inspect does subdomain exists and return 404 if it doesn't exist.

Upvotes: 3

Related Questions