[Django]-Django Context Processor Login In Issue

9👍

The indentation on your context processor seems to be off, it doesn’t match your description. I assume that the return statement is inside the if statement, since it matches both your description and the traceback.

The docs say (emphasis mine):

A context processor has a very simple interface: It’s a Python function that takes one argument, an HttpRequest object, and returns a dictionary that gets added to the template context. Each context processor must return a dictionary.

So if you don’t want to add anything to the context, your processor must return an empty dictionary instead of None:

def add_variable_to_context(request):
    if(request.user.id):
        profile = Profile.objects.filter(user_id = request.user.id).first()
        return {
            'main_profile': profile
        }
    return {}
👤knbk

Leave a comment