[Django]-How to get the current application name from a context processor?

4๐Ÿ‘

โœ…

A nice place to figure what is the current view is the process_view() middleware method, which also happens to be a great place to add a request variable. E.g.:

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

Will set request.current_app to testapp if view_func is testapp.views.some_view. It might not be bullet proof, some apps have views in submodules etc โ€ฆ But this should be a nice starter for you to tweak to your project specific needs.

๐Ÿ‘คjpic

Leave a comment