Hi! I think you've found the wrong mailing list for this post. This mailing list is for discussing the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy.
For support, please follow the "Getting Help" page: https://docs.djangoproject.com/en/3.0/faq/help/ . This will help you find people who are willing to support you, and to ask your question in a way that makes it easy for them to answer. Thanks for your understanding and all the best, Adam On Sat, 20 Jun 2020 at 17:41, Ibar Osman Ibrahim <ibaros...@gmail.com> wrote: > Request Method: GET > Request URL: http://localhost:8000/ > Django Version: 3.0.7 > Exception Type: AttributeError > Exception Value: > > module 'json' has no attribute 'JSONDecoder' > > Exception Location: > E:\Blogs\ll_env\lib\site-packages\django\contrib\messages\storage\cookie.py > in <module>, line 27 > Python Executable: E:\Blogs\ll_env\Scripts\python.exe > Python Version: 3.8.3 > Python Path: > > ['E:\\Blogs', > 'C:\\Python\\python38.zip', > 'C:\\Python\\DLLs', > 'C:\\Python\\lib', > 'C:\\Python', > 'E:\\Blogs\\ll_env', > 'E:\\Blogs\\ll_env\\lib\\site-packages'] > > Server time: Sat, 20 Jun 2020 13:28:59 +0000 > > > > > > > > > import json > > from django.conf import settings > from django.contrib.messages.storage.base import BaseStorage, Message > from django.http import SimpleCookie > from django.utils.crypto import constant_time_compare, salted_hmac > from django.utils.safestring import SafeData, mark_safe > > > class MessageEncoder(json.JSONEncoder): > """ > Compactly serialize instances of the ``Message`` class as JSON. > """ > message_key = '__json_message' > > def default(self, obj): > if isinstance(obj, Message): > # Using 0/1 here instead of False/True to produce more compact > json > is_safedata = 1 if isinstance(obj.message, SafeData) else 0 > message = [self.message_key, is_safedata, obj.level, obj.message] > if obj.extra_tags: > message.append(obj.extra_tags) > return message > return super().default(obj) > > > class MessageDecoder(json.JSONDecoder): > """ > Decode JSON that includes serialized ``Message`` instances. > """ > > def process_messages(self, obj): > if isinstance(obj, list) and obj: > if obj[0] == MessageEncoder.message_key: > if len(obj) == 3: > # Compatibility with previously-encoded messages > return Message(*obj[1:]) > if obj[1]: > obj[3] = mark_safe(obj[3]) > return Message(*obj[2:]) > return [self.process_messages(item) for item in obj] > if isinstance(obj, dict): > return {key: self.process_messages(value) > for key, value in obj.items()} > return obj > > def decode(self, s, **kwargs): > decoded = super().decode(s, **kwargs) > return self.process_messages(decoded) > > > class CookieStorage(BaseStorage): > """ > Store messages in a cookie. > """ > cookie_name = 'messages' > # uwsgi's default configuration enforces a maximum size of 4kb for all the > # HTTP headers. In order to leave some room for other cookies and headers, > # restrict the session cookie to 1/2 of 4kb. See #18781. > max_cookie_size = 2048 > not_finished = '__messagesnotfinished__' > > def _get(self, *args, **kwargs): > """ > Retrieve a list of messages from the messages cookie. If the > not_finished sentinel value is found at the end of the message list, > remove it and return a result indicating that not all messages were > retrieved by this storage. > """ > data = self.request.COOKIES.get(self.cookie_name) > messages = self._decode(data) > all_retrieved = not (messages and messages[-1] == self.not_finished) > if messages and not all_retrieved: > # remove the sentinel value > messages.pop() > return messages, all_retrieved > > def _update_cookie(self, encoded_data, response): > """ > Either set the cookie with the encoded data if there is any data to > store, or delete the cookie. > """ > if encoded_data: > response.set_cookie( > self.cookie_name, encoded_data, > domain=settings.SESSION_COOKIE_DOMAIN, > secure=settings.SESSION_COOKIE_SECURE or None, > httponly=settings.SESSION_COOKIE_HTTPONLY or None, > samesite=settings.SESSION_COOKIE_SAMESITE, > ) > else: > response.delete_cookie(self.cookie_name, > domain=settings.SESSION_COOKIE_DOMAIN) > > def _store(self, messages, response, remove_oldest=True, *args, **kwargs): > """ > Store the messages to a cookie and return a list of any messages which > could not be stored. > > If the encoded data is larger than ``max_cookie_size``, remove > messages until the data fits (these are the messages which are > returned), and add the not_finished sentinel value to indicate as > much. > """ > unstored_messages = [] > encoded_data = self._encode(messages) > if self.max_cookie_size: > # data is going to be stored eventually by SimpleCookie, which > # adds its own overhead, which we must account for. > cookie = SimpleCookie() # create outside the loop > > def stored_length(val): > return len(cookie.value_encode(val)[1]) > > while encoded_data and stored_length(encoded_data) > > self.max_cookie_size: > if remove_oldest: > unstored_messages.append(messages.pop(0)) > else: > unstored_messages.insert(0, messages.pop()) > encoded_data = self._encode(messages + [self.not_finished], > encode_empty=unstored_messages) > self._update_cookie(encoded_data, response) > return unstored_messages > > def _hash(self, value): > """ > Create an HMAC/SHA1 hash based on the value and the project setting's > SECRET_KEY, modified to make it unique for the present purpose. > """ > key_salt = 'django.contrib.messages' > return salted_hmac(key_salt, value).hexdigest() > > def _encode(self, messages, encode_empty=False): > """ > Return an encoded version of the messages list which can be stored as > plain text. > > Since the data will be retrieved from the client-side, the encoded > data > also contains a hash to ensure that the data was not tampered with. > """ > if messages or encode_empty: > encoder = MessageEncoder(separators=(',', ':')) > value = encoder.encode(messages) > return '%s$%s' % (self._hash(value), value) > > def _decode(self, data): > """ > Safely decode an encoded text stream back into a list of messages. > > If the encoded text stream contained an invalid hash or was in an > invalid format, return None. > """ > if not data: > return None > bits = data.split('$', 1) > if len(bits) == 2: > hash, value = bits > if constant_time_compare(hash, self._hash(value)): > try: > # If we get here (and the JSON decode works), everything > is > # good. In any other case, drop back and return None. > return json.loads(value, cls=MessageDecoder) > except json.JSONDecodeError: > pass > # Mark the data as used (so it gets removed) since something was wrong > # with the data. > self.used = True > return None > > -- > You received this message because you are subscribed to the Google Groups > "Django developers (Contributions to Django itself)" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to django-developers+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-developers/1eae0edd-2633-413c-8403-16165679829co%40googlegroups.com > <https://groups.google.com/d/msgid/django-developers/1eae0edd-2633-413c-8403-16165679829co%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- Adam -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAMyDDM1JiaZ4TK7f9bR9DLCvhNsTVZGhSPqczQ6B6AP%3DgxpBQA%40mail.gmail.com.