我想动态修改django表单中的字段选项。 因为项目的列表很长(超过650个项目),所以我将它们存储在django缓存中。你知道吗
但是,当我想将它们作为字段选择注入时,应用程序会变得没有响应(有时会返回ERR\u EMPTYRESPONSE)。你知道吗
我的观点:
class HomeView(TemplateView):
template_name = 'web/pages/homepage.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
categories = cache.get_or_set('categories_list', Category.objects.values('uuid', 'code', 'name'), 3600)
categories_choices = [(o['uuid'], '{} ({})'.format(o['name'], o['code'])) for o in categories]
print(categories_choices) #its printing proper choices here
context['form'] = SearchForm()
context['form'].fields['category'].choices = categories_choices #this line causes freeze/timeout
return context
你知道那里发生了什么吗?也许600多个项目作为下拉选择太多了?你知道吗
最好的方法是使用ajax。否则会出现一些浏览器加载问题。您可以使用ajax来实现这一点。你知道吗
你知道吗表单.py你知道吗
编写一个函数来返回请求类别的dict
在html模板中添加以下脚本
相关问题 更多 >
编程相关推荐