In my Django template I have a block cached:
{% cache 300 markers_list request.user.pk request.LANGUAGE_CODE %}
{% for marker in page_obj.object_list %}
{{ marker }}
{% endcache %}
I use a signal to invalidate a cache:
@receiver(signal=[post_save, post_delete], sender=Marker)
def clear_cache_block(sender, request, **kwargs):
key = make_template_fragment_key("markers_list", [request.user.pk, request.language_code])
cache.delete(key)
I do not understand how to get [request.user.pk, request.language_code].
If I add request to clear_cache_block(sender, request, **kwargs)
I get an error:
clear_cache_block() missing 1 required positional argument: 'request'
CodePudding user response:
I do not know is this a good solution or there are some better ones, so I handled it with this code:
from django.utils.translation import get_language
@receiver(signal=[post_save, post_delete], sender=Marker)
def clear_cache_block(sender, **kwargs):
user = kwargs["instance"].owner_id
user_language = get_language()
key = make_template_fragment_key("markers_list", [user, user_language])
cache.delete(key)
Or one more is:
@receiver(signal=[post_save, post_delete], sender=Marker)
def clear_cache_block(sender, instance, **kwargs):
user = instance.owner_id
user_language = get_language()
key = make_template_fragment_key("markers_list", [user, user_language])
cache.delete(key)
CodePudding user response:
request is passed as a first argument, so try to put the request in first like this:
clear_cache_block(request, sender, **kwargs)