[Answered ]-(Django) Passing 'context' (basic info) to every page, even forms?

0👍

While ndpu has correctly pointed you to context processors to solve the general problem, there doesn’t really seem to be any need in this case.

You already have a direct relationship between the current user and the relevant Player: I assume that is a one-to-one. And the user object is already passed to the template context by the default context processors, as long as you are using RequestContext (which you are, via the render shortcut.) So, without any extra context at all, you can simply do this in your template:

{{ user.player }}

to get the player.

2👍

You can write a custom context processor:

def foo(request):
    """ 
    Adds Player to context
    """

    context = {'p': None}
    if request.user.is_authenticated():
        p = Player.objects.get(user=request.user)
        if p:
            context['p'] = p

    return context

and add it to TEMPLATE_CONTEXT_PROCESSORS tuple in settings.py (assumed that foo placed in context_processors.py module in your application folder):

TEMPLATE_CONTEXT_PROCESSORS = (
    # ...
    "your_app_name.context_processors.foo",
)
👤ndpu

Leave a comment