我在项目目录中的另一个文件中有一个单独的缓存创建代码。。。
authentication.py
caches_folder = "./.spotify_caches/"
if not os.path.exists(caches_folder):
os.makedirs(caches_folder)
def session_cache_path():
return caches_folder + request.session.get("uuid")
oauth = SpotifyOAuth(
redirect_uri="http://127.0.0.1:8000/spotify/authorize",
scope='user-library-read',
show_dialog=True,
cache_path=session_cache_path()
)
所以我试图通过导入from .authentication import oauth
来使用views.py
中的oauth
views.py
def login(request):
if not request.session.get("uuid"):
request.session["uuid"] = str(uuid.uuid4())
authorize_url = oauth.get_authorize_url()
return redirect(authorize_url)
错误:return caches_folder + request.session.get("uuid") NameError: name 'request' is not defined
我认为这是因为request.session.get("uuid")
是在视图之外定义的,但我不想一直在单独的视图中创建oauth
。我该如何做到最好?
编辑:
def session_cache_path(uu_id):
return caches_folder + uu_id
oauth = SpotifyOAuth(
redirect_uri="http://127.0.0.1:8000/spotify/authorize",
scope='user-library-read',
show_dialog=True,
cache_path=session_cache_path(uu_id)
)
我在authentication.py
中执行了str(uuid.uuid4())
,并将值分配给oauth
中的函数调用,并将其导入到我的视图