我的 Django 项目运行良好。然后按照步骤将 Auth0 实施到我的项目中,我已在前端成功设置该项目。 我遵循了所有这些步骤: https://auth0.com/docs/quickstart/backend/django/01-authorization
在 Python 3.9.6 中运行('.venv': Pipenv)./.venv/bin/python 尝试在所有环境中安装要求
我什至将requirements.txt更新到最现代的版本,看看是否是兼容性问题。当我跑步时
python manage.py runserver
我得到
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/chasesheaff/teachr-backend/teachr/urls.py", line 23, in <module>
path('', include('auth0authorization.urls')),
File "/Users/chasesheaff/Library/Python/3.9/lib/python/site-packages/django/urls/conf.py", line 38, in include
urlconf_module = import_module(urlconf_module)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/chasesheaff/teachr-backend/auth0authorization/urls.py", line 3, in <module>
from . import views
File "/Users/chasesheaff/teachr-backend/auth0authorization/views.py", line 5, in <module>
import jwt
ModuleNotFoundError: No module named 'jwt'
pip freeze
产生这个结果
asgiref==3.7.2
certifi==2024.7.4
cffi==1.17.0
chardet==3.0.4
charset-normalizer==3.3.2
cryptography==43.0.0
Django==4.2.15
django-cors-headers==4.4.0
djangorestframework==3.10.3
drf-jwt==1.19.2
future==0.18.2
idna==2.8
macholib==1.15.2
pycparser==2.22
PyJWT==2.9.0
requests==2.32.3
six==1.15.0
sqlparse==0.4.4
typing_extensions==4.8.0
urllib3==1.25.11```
have jwt in ```utils.py```
```from django.contrib.auth import authenticate
import json
import jwt
import requests
def jwt_get_username_from_payload_handler(payload):
username = payload.get('sub').replace('|', '.')
authenticate(remote_user=username)
return username
... more code in file
并在
views.py
# Create your views here.
from functools import wraps
import jwt
from django.http import JsonResponse
def get_token_auth_header(request):
"""Obtains the Access Token from the Authorization Header
"""
auth = request.META.get("HTTP_AUTHORIZATION", None)
parts = auth.split()
token = parts[1]
return token
... more code
我是一名前端工程师,试图更深入地学习后端。因此,我将通过这个镜头进行解释,以帮助处于类似情况的其他人。
pip
类似于 npm
requirements.txt
类似于 package.json
更好的解释在这里:
Node 中的 npm 就像 Django 中的 virtualenv 一样吗?
我安装了 python 3.9 以及 3.11.5 我使用运行 python 3.9 的
python manage.py runserver
运行虚拟环境,但是当我使用 3.11.5 的 python3 manage.py runserver
时,一切都按预期工作。
浪费了几天的编码时间,但学到了很多关于虚拟环境的知识。这里的线程帮助找到了解决方案:https://discuss.python.org/t/python3-v3-x-vs-python-v3-x/29087