[Fixed]-Django Memcached displaying wrong logged in user

1👍

Looking at your templates, you are clearly caching the sidebar that contains details of the logged in user. It is not surprising that a later visitor to the site will see the cached fragment from an earlier, different user.

{% load cache %}
{% cache 500 right_sidebar %}
...
<h1>{{ request.user.first_name }}&nbsp;{{ request.user.last_name }}</h1>
...
{% endcache %}

The simplest fix is to remove the cache tag from this block. Alternatively, the docs suggest that you can include the username when using the cache tag and cache per-user. You’ll have fewer ‘cache hits’ when you do this, so only do it for blocks that contain user details.

{% load cache %}
{% cache 500 right_sidebar request.user.username %}
...
<h1>{{ request.user.first_name }}&nbsp;{{ request.user.last_name }}</h1>
...
{% endcache %}

Leave a comment