How do I get the current application name from the context processor?

I am trying to get the current name of the application (the application that contains the current view) from the context processor, because my entire application has its own Webdoor models, but they all have the same attributes (some values ​​are different). The values ​​from Webdoor are used on every page, so I wanted to get it inside my custom context processor to make it easier than calling it in all views. Not only that, but I could use it for other similar tasks. Once I already know the name of the model, I just need the application to call it using the get_model method.

Does anyone know how to do this?

Thank!

+3


source to share


1 answer


A good place to define what the current view is is the process_view () method of the middleware , which is also a great place to add a query variable. For example:.

class CurrentViewApplicationName(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        request.current_app = view_func.__module__.split('.')[0]

      



The request.current_app parameter will be set testapp

if view_func is equal testapp.views.some_view

. It might not be a bullet, some applications have views in submodules, etc. But it should be a good starter for you to customize your specific project needs.

+4


source







All Articles