Dynamic View Selection

32 views
Skip to first unread message

Khai Weng Au Yeong

unread,
Aug 11, 2017, 8:42:13 AM8/11/17
to Django users
Not sure if this has been covered before, but I was thinking about this for awhile.

Is there a way to dynamically select a View to be used by a URL without having to replicate the URL dispatcher over and over via a "superceding" like method. Like for example, in my project (let's say it's generic), if I have "BaseEmailer" at "<project_root>.emailer.views" and I have a client with a special-case need installed at "<project_root>.clients.emailer.views", can I get Django to dynamically select it just based off this logic: if client-app-name = base-app-name?

Russell Keith-Magee

unread,
Aug 11, 2017, 1:03:18 PM8/11/17
to Django Users
Yes - but it doesn’t depend on some hidden feature of Django. It’s 

A Django View is just a function that accepts a request + args, and returns a response. That view can, itself, call other functions - including other functions that can act as views. So, you could write a “switch statement” view:

def switch_view(request, arg1, arg2):
    if client_app_name == ‘xxx’:
        return xxx.view(request, arg1, arg2)
    else:
        return base_view(request, arg1, arg2)

The logic of exactly how to manage the “switch” is entirely up to you.

Depending on how you set up your client sub-packages, you might even be able to use import logic to your advantage:

def switch_view(request, arg1, arg2):
    try:
        client_module = importlib.import_module(‘project.%s.emailer.view’ % client_name)
        return client_module.view(request, arg1, arg2)
    except ImportError:
        return base_view(request, arg1, arg2)

Yours,
Russ Magee %-)

On 11 Aug 2017, at 5:32 am, Khai Weng Au Yeong <aykha...@gmail.com> wrote:

Not sure if this has been covered before, but I was thinking about this for awhile.

Is there a way to dynamically select a View to be used by a URL without having to replicate the URL dispatcher over and over via a "superceding" like method. Like for example, in my project (let's say it's generic), if I have "BaseEmailer" at "<project_root>.emailer.views" and I have a client with a special-case need installed at "<project_root>.clients.emailer.views", can I get Django to dynamically select it just based off this logic: if client-app-name = base-app-name?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bc82a384-dec5-4f0a-8cb4-2e4c642b3677%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Khai Weng Au Yeong

unread,
Aug 13, 2017, 10:47:53 PM8/13/17
to Django users
Hey, Russ.

Alright, that answers plenty. If I were to do this I think I would experiment with implementing this into the Middleware segment.


Thanks,
Khai
Reply all
Reply to author
Forward
0 new messages