首页 > 解决方案 > 如何将缓存设置导入views.py

问题描述

我在我的项目目录的另一个文件中有一个单独的缓存创建代码......

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()
)

所以我试图views.py通过导入来使用 oauthfrom .authentication import 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)
)

标签: pythondjangooauth-2.0django-views

解决方案


我做了一个str(uuid.uuid4())输入authentication.py并将值分配给函数调用oauth并将其导入我的视图


推荐阅读