无效的base64编码的字符串:数据字符的数目217不能为1大于4的倍数

如何解决无效的base64编码的字符串:数据字符的数目217不能为1大于4的倍数

我正在学习django,并使用它创建一个todo应用。同时设置django rest框架api。我收到一个异常错误。我使用django已有一段时间,但之前从未遇到过相同的错误。我不知道为什么会出现这个错误。

当我第一次执行命令manage.py runserver并导航到用户时,就会发生该错误

错误如下

Invalid base64-encoded string: number of data characters (217) cannot be 1 more than a multiple of 4
Request Method: GET
Request URL:    http://127.0.0.1:8001/users/
Django Version: 3.1
Exception Type: Error
Exception Value:    
Invalid base64-encoded string: number of data characters (217) cannot be 1 more than a multiple of 4
Exception Location: /usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/base64.py,line 87,in b64decode
Python Executable:  /Users/yogendrakumar/PycharmProjects/todo_app/bin/python
Python Version: 3.8.5
Python Path:    
['/Users/yogendrakumar/PycharmProjects/todo_app','/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python38.zip','/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8','/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload','/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages']
Server time:    Sat,29 Aug 2020 12:25:01 +0530

views.py

from django.contrib.auth.models import User,Group
from rest_framework import viewsets
from rest_framework import permissions
from todo.serializers import UserSerializer,GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]

以下是追溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8001/users/

Django Version: 3.1
Python Version: 3.8.5
Installed Applications:
['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','rest_framework','todo']
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']



Traceback (most recent call last):
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py",line 215,in _get_session
    return self._session_cache

During handling of the above exception ('SessionStore' object has no attribute '_session_cache'),another exception occurred:
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py",line 118,in decode
    return signing.loads(session_data,salt=self.key_salt,serializer=self.serializer)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/core/signing.py",line 135,in loads
    base64d = TimestampSigner(key,salt=salt).unsign(s,max_age=max_age).encode()
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/core/signing.py",line 201,in unsign
    result = super().unsign(value)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/core/signing.py",line 184,in unsign
    raise BadSignature('Signature "%s" does not match' % sig)

During handling of the above exception (Signature "xnqTuv_ylPs2HNImqZUFHZYYDRY5IfETbWXc5_4zbB8" does not match),another exception occurred:
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/core/handlers/exception.py",line 47,in inner
    response = get_response(request)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/core/handlers/base.py",line 179,in _get_response
    response = wrapped_callback(request,*callback_args,**callback_kwargs)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/views/decorators/csrf.py",line 54,in wrapped_view
    return view_func(*args,**kwargs)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/viewsets.py",line 114,in view
    return self.dispatch(request,*args,**kwargs)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/views.py",line 505,in dispatch
    response = self.handle_exception(exc)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/views.py",line 465,in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/views.py",line 476,in raise_uncaught_exception
    raise exc
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/views.py",line 493,in dispatch
    self.initial(request,line 410,in initial
    self.perform_authentication(request)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/views.py",line 324,in perform_authentication
    request.user
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/request.py",line 220,in user
    self._authenticate()
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/request.py",line 373,in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/rest_framework/authentication.py",line 123,in authenticate
    if not user or not user.is_active:
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/utils/functional.py",line 240,in inner
    self._setup()
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/utils/functional.py",line 376,in _setup
    self._wrapped = self._setupfunc()
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/auth/middleware.py",line 23,in <lambda>
    request.user = SimpleLazyObject(lambda: get_user(request))
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/auth/middleware.py",line 11,in get_user
    request._cached_user = auth.get_user(request)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/auth/__init__.py",line 174,in get_user
    user_id = _get_user_session_key(request)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/auth/__init__.py",line 58,in _get_user_session_key
    return get_user_model()._meta.pk.to_python(request.session[SESSION_KEY])
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py",line 65,in __getitem__
    return self._session[key]
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py",in _get_session
    self._session_cache = self.load()
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/db.py",line 44,in load
    return self.decode(s.session_data) if s else {}
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py",line 122,in decode
    return self._legacy_decode(session_data)
  File "/Users/yogendrakumar/PycharmProjects/todo_app/lib/python3.8/site-packages/django/contrib/sessions/backends/base.py",line 126,in _legacy_decode
    encoded_data = base64.b64decode(session_data.encode('ascii'))
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/base64.py",in b64decode
    return binascii.a2b_base64(s)

Exception Type: Error at /users/
Exception Value: Invalid base64-encoded string: number of data characters (217) cannot be 1 more than a multiple of 4

serializers.py

from django.contrib.auth.models import User,Group
from rest_framework import serializers


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url','username','email','groups']


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url','name']

解决方法

我遇到了同样的问题,但是当我的Django版本为3.0.8并将其升级到3.1时,我得到了错误消息

,

真的不知道为什么,但是从浏览器中清除 cookie 可以解决问题。

,

随时清除浏览器缓存和 cookie。

,

DJANGO_SESSION表中删除所有旧会话

,

我忘记激活我的virtualenv并收到此错误,因为我正在使用错误的Django版本运行此项目 所以我激活并解决了

,

清除浏览器缓存,然后重试。 :)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-