Django - accessing the RequestContext from within a custom filter

前端 未结 4 1945
花落未央
花落未央 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 03:06

    A somehow less hacky solution to Daniel Rhoden's proposal is, to use threading.local(). Define a middleware class, which stores your request as a global object inside your local thread, and add that class to your MIDDLEWARE_CLASSES.

    Now a template filter can easily access that request object.

提交回复
热议问题