Django - accessing the RequestContext from within a custom filter

前端 未结 4 1984
花落未央
花落未央 2021-01-04 02:33

I\'ve got a filter currency, which takes a value in USD and converts it to a currency (either USD or GBP). The currency to convert to is stored in the session,

4条回答
  •  忘掉有多难
    2021-01-04 02:56

    I would have to agree with Adam that migrating the code to a custom tag is the best way.

    However, a client needed to record the use of certain filters only when a page was published and had a HUGE inventory of templates that used the existing filter syntax. It would have been a costly undertaking to rewrite all the templates. So, I came up with this simple function that extracts the context from the call stack:

    https://gist.github.com/drhoden/e05292e52fd5fc92cc3b

    def get_context(max_depth=4):
        import inspect
        stack = inspect.stack()[2:max_depth]
        context = {}
        for frame_info in stack:
            frame = frame_info[0]
            arg_info = inspect.getargvalues(frame)
            if 'context' in arg_info.locals:
                context = arg_info.locals['context']
                break
        return context
    

    Be sure to read my warnings, but this DOES give standard filters access to the context (when it is available) WITHOUT having to turn your filter into a tag.

提交回复
热议问题