【问题标题】:from .exceptions import InvalidKeyError ModuleNotFoundError: No module named 'jwt.exceptions'从 .exceptions 导入 InvalidKeyError ModuleNotFoundError:没有名为“jwt.exceptions”的模块
【发布时间】:2021-07-27 19:41:54
【问题描述】:

我正在尝试向注册用户发送电子邮件验证链接,但我不断收到此错误,我不知道为什么,但每当我向此端点发送发布请求时,我都会收到此错误

Internal Server Error: /auth/register                                                                                                                                    
Traceback (most recent call last):                                                                                                                                       
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner                        
    response = get_response(request)                                                                                                                                     
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response                    
    response = wrapped_callback(request, *callback_args, **callback_kwargs)                                                                                              
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view                   
    return view_func(*args, **kwargs)                                                                                                                                    
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view                              
    return self.dispatch(request, *args, **kwargs)                                                                                                                       
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch                              
    response = self.handle_exception(exc)                                                                                                                                
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception                      
    self.raise_uncaught_exception(exc)                                                                                                                                   
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception              
    raise exc                                                                                                                                                            
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch                              
    response = handler(request, *args, **kwargs)                                                                                                                         
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/SRC/users/views.py", line 28, in post                                                                         
    print(token)                                                                                                                                                         
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework_simplejwt/tokens.py", line 80, in __str__                     
    from .state import token_backend                                                                                                                                     
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework_simplejwt/state.py", line 3, in <module>                      
    from .backends import TokenBackend                                                                                                                                   
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/rest_framework_simplejwt/backends.py", line 3, in <module>                   
    from jwt import InvalidAlgorithmError, InvalidTokenError, algorithms                                                                                                 
  File "/mnt/c/Users/Somtochukwu/Desktop/cultural-exchange/proj/lib/python3.8/site-packages/jwt/algorithms.py", line 5, in <module>                                      
    from .exceptions import InvalidKeyError                                                                                                                              
ModuleNotFoundError: No module named 'jwt.exceptions'  

这是我的意见.py

class RegisterView(GenericAPIView):
    serializer_class = RegisterSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            serializer.save()
            user_data = serializer.data 
            user = User.objects.get(email=user_data['email'])
            token = RefreshToken.for_user(user).access_token
            print(token)
            current_site = get_current_site(request).domain
            relative_link = reverse('verify')
            absurl = 'http://'+ current_site + relative_link + "?token=" + str(token)
            email_body = f"Hi {user_name}. Use the link below to verify your email \n {absurl}" 
            email = send_mail("Account confirmation mail", email_body, settings.EMAIL_HOST_USER, [user.email])
            

            return Response(user_data, status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


在我不断收到错误之前我已经安装了 JWT

这是我的点子列表

Package                       Version                                                                                                                                    
----------------------------- -------                                                                                                                                    
asgiref                       3.3.4                                                                                                                                      
cffi                          1.14.5                                                                                                                                     
cryptography                  3.4.7                                                                                                                                      
Django                        3.2                                                                                                                                        
django-braces                 1.14.0                                                                                                                                     
django-countries              7.1                                                                                                                                        
django-rest-framework         0.1.0                                                                                                                                      
djangorestframework           3.12.4                                                                                                                                     
djangorestframework-simplejwt 4.6.0                                                                                                                                      
Pillow                        8.2.0                                                                                                                                      
pip                           21.0.1                                                                                                                                     
pycparser                     2.20                                                                                                                                       
PyJWT                         2.0.1                                                                                                                                      
pytz                          2021.1                                                                                                                                     
setuptools                    54.1.2                                                                                                                                     
six                           1.15.0                                                                                                                                     
sqlparse                      0.4.1                                                                                                                                      
wheel                         0.36.2

请问这个错误的原因是什么,我该如何解决它

【问题讨论】:

  • 你在settings.py下添加到你的applications\middleware了吗?
  • 我没有添加任何东西,我应该添加什么?
  • 我已经添加了它我仍然得到同样的错误

标签: python django django-rest-framework django-rest-framework-jwt django-rest-framework-simplejwt


【解决方案1】:

您应该将此添加到settings.py

REST_FRAMEWORK = {
    ...
    'DEFAULT_AUTHENTICATION_CLASSES': (
        ...
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    )
    ...
}

根据docs

【讨论】:

  • 我已经添加了它我仍然得到同样的错误
  • 参考这个answer,使用 PyJwt 和 simple-jwt 会导致命名空间问题
【解决方案2】:

Django 混淆了模块 'jwt' 和 PyJWT。为避免这种混淆,请卸载这两个模块。现在,先安装 jwt,然后再安装 PyJWT。

pip install jwt
pip install pyjwt

安装顺序很重要

【讨论】:

  • 如果这是真的,它不应该。你能把你的研究带到相应的github repo吗?只是一个建议。
猜你喜欢
  • 2021-05-16
  • 2019-04-12
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2020-02-25
  • 2021-12-14
  • 2022-01-07
相关资源
最近更新 更多