[Fixed]-How to get request.user in a code, which does not have any relation to views?

1๐Ÿ‘

โœ…

I just answered this same question (albeit relating to forms) in another question. Please see my answer there.

Assuming you have setup threadlocals.py as detailed in that post, and stored it as sitetree/threadlocals.py:

from sitetree.sitetreeapp import register_items_hook
from sitetree.threadlocals import get_current_user

def my_items_processor(tree_items, tree_sender):

    user = get_current_user()

    if tree_sender == 'menu.children':
        for item in tree_items:
            if user.username:
                item.url = user.username
            else:
                item.url = user.pk

    return tree_items
๐Ÿ‘คAdam Hopkins

Leave a comment