django 从.exceptions导入无效键错误模块未发现错误:没有名为“jwt.exceptions”的模块

kknvjkwl  于 2022-11-26  发布在  Go
关注(0)|答案(4)|浏览(213)

我尝试向注册用户发送电子邮件验证链接,但我一直收到此错误,我不知道为什么,但每当我向此端点发送帖子请求时,我总是收到此错误

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'

这里是我的views.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

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

2ledvvac

2ledvvac1#

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

pip install jwt
pip install pyjwt

安装顺序事项

tzcvj98z

tzcvj98z2#

您应该将其添加到settings.py

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

根据文件

6qfn3psc

6qfn3psc3#

而不是使用:

token = RefreshToken.for_user(user).access_token

用途:

from rest_framework_simplejwt.tokens import AccessToken

token = AccessToken.for_user(user)

对我很有效

sqserrrh

sqserrrh4#

我添加了一个答案,以供将来参考。正如之前另一个用户所指出的,Django在模块jwtPyJWT之间混淆了。您必须卸载这两个模块,然后重新安装PyJWT

pip install jwt
pip install pyjwt

您还需要将以下代码添加到settings.py中。

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

然而,我发现在我自己的应用程序中,这并不完全足够。尽管在一个专用的虚拟环境中启动Django应用程序,它将默认为全局python环境。我发现这是一个奇怪的交互,需要一些时间来修复。为了快速和更容易地修复这个问题,请遵循全局环境中的卸载和安装说明,然而,这并不是最好的做法,最好是确保Django在预期的环境中运行。

相关问题