django-cookie-consent icon indicating copy to clipboard operation
django-cookie-consent copied to clipboard

Can't pickle <function QuerySet._clone at 0x000001EAA19615E0>: it's not the same object as django.db.models.query.QuerySet._clone

Open jurezakrajsek opened this issue 2 years ago • 9 comments

Trying to implement this nice coockie-consent library to my page but i get an error.

I have tryed to recreate the same values in the CookieGroup and Cookies tables and added the test_page.html and TestPageView to my page just to see if it is working. When i access the TestPageView i get the following error:

Template error:
In template D:\Jure\django-apps\cobs_dev\cobs\templates\cobs\test_page.html, error at line 71
   Can't pickle <function QuerySet._clone at 0x000001EAA19615E0>: it's not the same object as django.db.models.query.QuerySet._clone
   61 :     <script type="{% js_type_for_cookie_consent request "social" "*:.google.com" %}" data-varname="social">
   62 :       (function() {
   63 :         var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
   64 :         po.src = 'https://apis.google.com/js/plusone.js';
   65 :         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
   66 :       })();
   67 :     </script>
   68 : 
   69 : 
   70 :     {% if request|cookie_consent_enabled %}
   71 :        {% not_accepted_or_declined_cookie_groups request as cookie_groups %}
   72 : 
   73 :       {% if cookie_groups %}
   74 :         {% url "cookie_consent_cookie_group_list" as url_cookies %}
   75 :         {% cookie_consent_accept_url cookie_groups as url_accept %}
   76 :         {% cookie_consent_decline_url cookie_groups as url_decline %}
   77 :         <script type="text/javascript">
   78 :           var cookie_groups = [];
   79 :           {% for cookie_group in cookie_groups %}
   80 :             cookie_groups.push("{{ cookie_group.varname }}");
   81 :           {% endfor %}


Traceback (most recent call last):
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\test\utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\templatetags\cookie_consent_tags.py", line 61, in not_accepted_or_declined_cookie_groups
    return get_not_accepted_or_declined_cookie_groups(request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\util.py", line 134, in get_not_accepted_or_declined_cookie_groups
    return [cookie_group for cookie_group in get_cookie_groups()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\util.py", line 81, in get_cookie_groups
    return all_cookie_groups().values()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\cache.py", line 25, in all_cookie_groups
    cache.set(CACHE_KEY, items, CACHE_TIMEOUT)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\cache.py", line 31, in _decorator
    return method(self, *args, **kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\cache.py", line 80, in set
    return self.client.set(*args, **kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\client\default.py", line 143, in set
    nvalue = self.encode(value)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\client\default.py", line 457, in encode
    value = self._serializer.dumps(value)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\serializers\pickle.py", line 29, in dumps
    return pickle.dumps(value, self._pickle_version)

Exception Type: PicklingError at /cookies/
Exception Value: Can't pickle <function QuerySet._clone at 0x000001EAA19615E0>: it's not the same object as django.db.models.query.QuerySet._clone

This tamplate tag seems to be the source of this error: {% not_accepted_or_declined_cookie_groups request as cookie_groups %}

I'm running the website on: Djnago 3.2.13 django-redis 5.2.0

jurezakrajsek avatar Apr 15 '22 11:04 jurezakrajsek

Make sure you are placing this entire highlighted code into your template:

https://github.com/jazzband/django-cookie-consent/blob/master/tests/core/templates/test_page.html#L58-L108

Also don't forget to put this at the top of your template: {% load cookie_consent_tags %}

{% not_accepted_or_declined_cookie_groups request as cookie_groups %} will set the cache key in redis or your cache backend if the cache key does not exist. It will do this when you first load your page containing this template tag. Here is an example of what the cache key will look like: ":1:cookie_consent_cache". If you don't need the cache feature, then you can remove the template tag if you want. However, you will miss out on the speed of the cache if you use things like the CleanCookiesMiddleware.

That template tag follows this flow:

  1. def not_accepted_or_declined_cookie_groups(request)
  2. def get_not_accepted_or_declined_cookie_groups(request):
  3. def get_cookie_groups(varname=None)
  4. def all_cookie_groups()

https://github.com/jazzband/django-cookie-consent/blob/master/cookie_consent/templatetags/cookie_consent_tags.py#L56 https://github.com/jazzband/django-cookie-consent/blob/master/cookie_consent/util.py#L130 https://github.com/jazzband/django-cookie-consent/blob/master/cookie_consent/util.py#L79 https://github.com/jazzband/django-cookie-consent/blob/master/cookie_consent/cache.py#L26

I am not familiar with the concept of pickling so I am not sure what to do here.

Make sure your django-redis settings look something like this:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

Check if the cache is being set in redis:

redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> select 1
127.0.0.1:6379[1]> keys *
1) ":1:cookie_consent_cache"

Are you not getting the cache key set in redis? If you could provide more details, code, or more tracebacks it would be helpful.

some1ataplace avatar Apr 15 '22 15:04 some1ataplace

The settings.py is correct, only that i have redis on a virtual on the local network. The port there is opened for connection, and caching seems to be working for sorl-thumbnail.

> keys * 
does not return and :1:cookie_consent_cache, only sorl-thumbnail images are listed

I connected to the redis-cli and started MONITOR When visiting the test_page.html I get this in the MONITOR, which seems to be OK, so redis seems to be configured correctly and is accessible.

1650043901.661728 [1 192.168.10.3:50185] "GET" ":1:cookie_consent_cache"

urls.py

urlpatterns = [
    path('cookies/', views.TestPageView.as_view(), name='test_page'),

views.py

class TestPageView(TemplateView):
    template_name = "cobs/test_page.html"

    def get(self, request, *args, **kwargs):
        response = super(TestPageView, self).get(request, *args, **kwargs)
        if get_cookie_value_from_request(request, "optional") is True:
            val = "optional cookie set from django"
            response.set_cookie("optional_test_cookie", val)
        return response

test_page.html is copied from the test example

Traceback

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/cookies/

Django Version: 3.2.13
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'accounts.apps.AccountsConfig',
 'haystack',
 'django_extensions',
 'admin_reorder',
 'mathfilters',
 'active_link',
 'bootstrap4',
 'bootstrap_pagination',
 'tempus_dominus',
 'crispy_forms',
 'fontawesome_5',
 'django_select2',
 'multiselectfield',
 'chartjs',
 'ckeditor',
 'ckeditor_uploader',
 'import_export',
 'sorl.thumbnail',
 'django_celery_beat',
 'django_celery_results',
 'debug_toolbar',
 'nplusone.ext.django',
 'rest_framework',
 'celery_haystack',
 'cookie_consent']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'admin_reorder.middleware.ModelAdminReorder',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'nplusone.ext.django.NPlusOneMiddleware']

Template error:
In template D:\Jure\django-apps\cobs_dev\cobs\templates\cobs\test_page.html, error at line 72
   Can't pickle <function QuerySet._clone at 0x0000018097A984C0>: it's not the same object as django.db.models.query.QuerySet._clone
   62 :     <script type="{% js_type_for_cookie_consent request "social" "*:.google.com" %}" data-varname="social">
   63 :       (function() {
   64 :         var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
   65 :         po.src = 'https://apis.google.com/js/plusone.js';
   66 :         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
   67 :       })();
   68 :     </script>
   69 : 
   70 : 
   71 :     {% if request|cookie_consent_enabled %}
   72 :        {% not_accepted_or_declined_cookie_groups request as cookie_groups %} 
   73 : 
   74 :       {% if cookie_groups %}
   75 :         {% url "cookie_consent_cookie_group_list" as url_cookies %}
   76 :         {% cookie_consent_accept_url cookie_groups as url_accept %}
   77 :         {% cookie_consent_decline_url cookie_groups as url_decline %}
   78 :         <script type="text/javascript">
   79 :           var cookie_groups = [];
   80 :           {% for cookie_group in cookie_groups %}
   81 :             cookie_groups.push("{{ cookie_group.varname }}");
   82 :           {% endfor %}


Traceback (most recent call last):
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\test\utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\templatetags\cookie_consent_tags.py", line 61, in not_accepted_or_declined_cookie_groups
    return get_not_accepted_or_declined_cookie_groups(request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\util.py", line 134, in get_not_accepted_or_declined_cookie_groups
    return [cookie_group for cookie_group in get_cookie_groups()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\util.py", line 81, in get_cookie_groups
    return all_cookie_groups().values()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\cache.py", line 25, in all_cookie_groups
    cache.set(CACHE_KEY, items, CACHE_TIMEOUT)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\cache.py", line 31, in _decorator
    return method(self, *args, **kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\cache.py", line 80, in set
    return self.client.set(*args, **kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\client\default.py", line 143, in set
    nvalue = self.encode(value)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\client\default.py", line 457, in encode
    value = self._serializer.dumps(value)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\serializers\pickle.py", line 29, in dumps
    return pickle.dumps(value, self._pickle_version)

Exception Type: PicklingError at /cookies/
Exception Value: Can't pickle <function QuerySet._clone at 0x0000018097A984C0>: it's not the same object as django.db.models.query.QuerySet._clone

Thx for the help.

jurezakrajsek avatar Apr 15 '22 17:04 jurezakrajsek

Maybe because in redis running this command to get the key value for ":1:cookie_consent_cache":

127.0.0.1:6379[1]> get ":1:cookie_consent_cache"

Returns what appears to look like a pickled object.

To extract the value from this pickled object do this in the django shell command prompt:

pickle.loads(b"PICKLED_OBJECT_CACHE_VALUE_GOES_HERE")

And you will get:

{'Group1Cookie': <CookieGroup: Group1>, 'Group2Cookie': <CookieGroup: Group2>, 'Group3Cookie': <CookieGroup: Group3>}

Your traceback seems to point to this file:

https://github.com/jazzband/django-redis/blob/master/django_redis/serializers/pickle.py

The value we got above from the cache is inside of the dumps() function in that file. In that file, you can run print(value) and you will get the same thing.

https://github.com/jazzband/django-redis#advanced-usage

https://github.com/jazzband/django-redis#pluggable-serializer

Maybe try changing your settings.py for cache to use the JSON serializer instead of the pickle serializer that django-redis uses and report back. It seems like you can even write a custom serializer https://stackoverflow.com/a/59974128

I would also recommend to give us a way to recreate your error using the test app in this library if possible. There might be something in your project or even the database values for the cookie and cookiegroup tables causing this. Try running the test app in this library with django-redis and see if you get the same problem.

I am looking at your view and that seems fine but there are a few different ways you could write that without using library specific code.

if cookie_group_name in request.COOKIES.get('cookie_consent', None):
        response = HttpResponseRedirect(reverse('app:url_name')) #or even HttpResponse()
        response.set_cookie(settings.COOKIE_CONSENT_NAME, cookie_value, max_age)
        return response

some1ataplace avatar Apr 16 '22 03:04 some1ataplace

Thx for those suggestions. I tried now to change the redis serialazer to JSON, flushedall in redis, but also that does not help as i get the following error:

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/cookies/

Django Version: 3.2.13
Python Version: 3.8.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.humanize',
 'accounts.apps.AccountsConfig',
 'haystack',
 'django_extensions',
 'admin_reorder',
 'mathfilters',
 'active_link',
 'bootstrap4',
 'bootstrap_pagination',
 'tempus_dominus',
 'crispy_forms',
 'fontawesome_5',
 'django_select2',
 'multiselectfield',
 'chartjs',
 'ckeditor',
 'ckeditor_uploader',
 'import_export',
 'sorl.thumbnail',
 'django_celery_beat',
 'django_celery_results',
 'debug_toolbar',
 'nplusone.ext.django',
 'rest_framework',
 'celery_haystack',
 'cookie_consent']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'admin_reorder.middleware.ModelAdminReorder',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'nplusone.ext.django.NPlusOneMiddleware']


Template error:
In template D:\Jure\django-apps\cobs_dev\cobs\templates\cobs\test_page.html, error at line 72
   Object of type CookieGroup is not JSON serializable
   62 :     <script type="{% js_type_for_cookie_consent request "social" "*:.google.com" %}" data-varname="social">
   63 :       (function() {
   64 :         var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
   65 :         po.src = 'https://apis.google.com/js/plusone.js';
   66 :         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
   67 :       })();
   68 :     </script>
   69 : 
   70 : 
   71 :     {% if request|cookie_consent_enabled %}
   72 :        {% not_accepted_or_declined_cookie_groups request as cookie_groups %} 
   73 :       {% if cookie_groups %}
   74 :         {% url "cookie_consent_cookie_group_list" as url_cookies %}
   75 :         {% cookie_consent_accept_url cookie_groups as url_accept %}
   76 :         {% cookie_consent_decline_url cookie_groups as url_decline %}
   77 :         <script type="text/javascript">
   78 :           var cookie_groups = [];
   79 :           {% for cookie_group in cookie_groups %}
   80 :             cookie_groups.push("{{ cookie_group.varname }}");
   81 :           {% endfor %}
   82 : 


Traceback (most recent call last):
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\test\utils.py", line 100, in instrumented_test_render
    return self.nodelist.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\defaulttags.py", line 315, in render
    return nodelist.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\template\library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\templatetags\cookie_consent_tags.py", line 61, in not_accepted_or_declined_cookie_groups
    return get_not_accepted_or_declined_cookie_groups(request)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\util.py", line 134, in get_not_accepted_or_declined_cookie_groups
    return [cookie_group for cookie_group in get_cookie_groups()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\util.py", line 81, in get_cookie_groups
    return all_cookie_groups().values()
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\cookie_consent\cache.py", line 27, in all_cookie_groups
    cache.set(CACHE_KEY, items, CACHE_TIMEOUT)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\cache.py", line 31, in _decorator
    return method(self, *args, **kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\cache.py", line 80, in set
    return self.client.set(*args, **kwargs)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\client\default.py", line 143, in set
    nvalue = self.encode(value)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\client\default.py", line 457, in encode
    value = self._serializer.dumps(value)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django_redis\serializers\json.py", line 13, in dumps
    return json.dumps(value, cls=self.encoder_class).encode()
  File "C:\Python38\lib\json\__init__.py", line 234, in dumps
    return cls(
  File "C:\Python38\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python38\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "D:\Jure\django-apps\cobs_env\lib\site-packages\django\core\serializers\json.py", line 105, in default
    return super().default(o)
  File "C:\Python38\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '

Exception Type: TypeError at /cookies/
Exception Value: Object of type CookieGroup is not JSON serializable

jurezakrajsek avatar Apr 16 '22 06:04 jurezakrajsek

I am not sure. My guess is it has something to do with your code. When I use django-redis with my own project and the test app that comes with this library I do not get any problems with the latest django version. Try doing the following with the django-cookie-consent test app and see if you can reproduce the same error:

Download the zip file of django-cookie-consent cd Downloads/django-cookie-consent-master/tests/ Insert into ~/Downloads/django-cookie-consent-master/tests/settings.py the cache setting in an above comment I shared Install django-redis into the virtual environment Install other dependencies https://github.com/jazzband/django-cookie-consent/tree/master/requirements python manage.py runserver Load the 127.0.0.1:8000 home page redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> select 1 keys * 127.0.0.1:6379[1]> get ":1:cookie_consent_cache" pickle.loads(b"PICKLED_OBJECT_CACHE_VALUE_GOES_HERE") {'social': <CookieGroup: Social media>, 'optional': <CookieGroup: Optional (test) cookies>}

My advice is to use print() in both django-cookie-consent and django-redis and see what is going on in some of the files I shared above. Also try deleting stuff to try and narrow down the issue until you find the cause. What values are you passing into the django-redis serializer? What types are they? What values do you have in your database?

some1ataplace avatar Apr 17 '22 03:04 some1ataplace

I set-up clean cookie-consent testapp and set-up the CACHE to my redis server and all is running nicely. It seems that there is really something wrong with my settings, or a conflict.

In the database there are the same values as for the testapp. Cookies table:

1,sessionid,"",/,"",2022-04-19 12:10:16.295731 +00:00,1
2,csrftoken,"",/,"",2022-04-19 12:10:43.289858 +00:00,1
3,*,This cookies are used by Facebook to allow sharing content.,/,.google.com,2022-04-19 12:11:11.460622 +00:00,2
4,optional_test_cookie,"",/,"",2022-04-19 12:11:29.846639 +00:00,3

Cookies Groups table:

1,required,Required cookies,This cookies are required for website.,true,false,0,2022-04-19 12:09:09.028946 +00:00
2,social,Social media,Google Plus need to know who you are to work properly.,false,true,0,2022-04-19 12:09:32.494167 +00:00
3,optional,Optional (test) cookies,"",false,true,0,2022-04-19 12:09:49.587471 +00:00

I will try to do some printouts from the django-redis and cookie-consent and perhaps remove some stuff from the config file to narrow the issue down.

Did some further testing I copied the complete settings.py from myproject to the testapp. It was working OK at first, but as soon as I FLUSHALL cache on the redis server i get the Pickle error.

jurezakrajsek avatar Apr 19 '22 10:04 jurezakrajsek

Maybe it is a middleware you are using that is causing the problem. Django-cookie-consent has had issues where it would not work with the django-honeypot middleware for example. I would keep trying to troubleshoot it with print() and print(type()) to see what is going on. I am interested to see what the problem is.

some1ataplace avatar Apr 19 '22 14:04 some1ataplace

In did it seems to be the django-debuging-toolbar that makes the issues. When disabled i can run the django-cookie-consent normally. Now I just need to configure the cookiegroups/cookies and make a banner template to implement this on my project website.

Are there any example templates that would use bootstrap4 and the toggle buttons to accept/decline individual cookie settings? Thank you for your help

jurezakrajsek avatar Apr 20 '22 06:04 jurezakrajsek

Thanks for finding the cause of the issue.

Maybe you can view the source code of something like this from one of our maintainers MrCordeiro

https://pluckd.co/en/cookies/

There is not really an example template in this library but I think it would be great if you shared it with us if you can figure it out.

some1ataplace avatar Apr 20 '22 14:04 some1ataplace

Since it seems the error was caused by Django-Debug-Toolbar, I'm going to close this issue to clean up the backlog.

Feel free to reopen if I missed something and this should not have been closed yet!

sergei-maertens avatar Jun 11 '23 15:06 sergei-maertens

it happened to me using nplusone requirement

pulse-mind avatar Nov 27 '23 17:11 pulse-mind

Do you have a minimal reproducible example? Is there any reason to believe that cookie-consent is the culprit and not the other library?

edit: it appears that other people in this thread also had nplusone middleware, which monkeypatches queryset methods. Django itself supports caching entire querysets, so third party libraries breaking this behaviour are the culprit.

sergei-maertens avatar Nov 27 '23 17:11 sergei-maertens

I am sorry I did not give enough information. I do not have cookie-consent requirement but I have nplusone, so may be the original issue was due to this library. I wanted to add this information to help the user.

pulse-mind avatar Nov 27 '23 17:11 pulse-mind

I understand, it's much appreciated! It confirms our suspicions that this problem is not in cookie consent

sergei-maertens avatar Nov 27 '23 17:11 sergei-maertens