gpt4 book ai didi

python - Django:简化 View

转载 作者:太空宇宙 更新时间:2023-11-04 10:52:29 25 4
gpt4 key购买 nike

我有以下简单的看法。有什么方法可以让它更有活力吗?

# ...

if 'folding' not in request.session:
request.session['folding'] = {'shop':False,'users':False,'chat':False}

# ...

def update_folding_view(request,category,is_folded):
if request.is_ajax():
folding = request.session['folding']
if 'shop' in category:
folding.shop = is_folded
if 'users' in category:
folding.users = is_folded
if 'chat' in category:
folding.chat = is_folded
request.session['folding'] = folding
else:
raise Http404

最佳答案

这是我得到的:

from collections import defaultdict

...

if 'folding' not in request.session:
request.session['folding'] = defaultdict(bool)

...

def update_folding_view(request, category, is_folded):
if not request.is_ajax():
raise Http404
for item in category:
request.session['folding'][item] = is_folded

关于python - Django:简化 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com