python - 如何将缓存设置导入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)
)
解决方案
我做了一个str(uuid.uuid4())
输入authentication.py
并将值分配给函数调用oauth
并将其导入我的视图
推荐阅读
- c++ - 我应该如何解除分配使用新存储在 std::stack 上的指针
- python - Python中字符串的时间复杂度
- php - 如何更改 URL 格式后分页 Wordpress (Wp_Link_pages)
- java - Android:使用 CilpDrawable 水平裁剪所选图像不起作用?
- javascript - 使画布适合视口但保持图像分辨率
- php - 在 Docker 中与 PHP Swoole 一起运行 Blackfire 时出现问题
- python - Selenium find_element_by_id 不返回完整元素
- reactjs - 带有打字稿的故事书没有将 tsx 文件称为打字稿文件-“解析错误:缺少分号”
- python - Python 中的线性回归在 y=mx+c 中为较大的 x 值炸毁了梯度
- python - 如何切换在变量中声明的for循环