[英]Complex objects passed as args/kwargs between class based views in Django
I currently have a SessionWizardView from formtools package implemented. 我目前有一个来自formtools包的SessionWizardView实现。
class DataframeFormView(SessionWizardView):
def done(self,form_list):
params={}
for form in form_list:
params.update(form.cleaned_data)
return HttpResponseRedirect(reverse('download',kwargs={'params':params}))
Everything is working fine in the forms view, and when the forms are successfully filled the above mentioned function runs, however I need to pass the params argument to another view: 在窗体视图中,一切都工作正常,并且在成功填充窗体时,上述功能将运行,但是我需要将params参数传递给另一个视图:
from django_downloadview import VirtualDownloadView, VirtualFile
class DataframeDownloadView(VirtualDownloadView):
def get_file(self):
### Access params dictionary here!!!!
return VirtualFile(tfile.name, name=tfile.name)
Thing I've tried: 我尝试过的事情:
1) override __init__
to add self.params = kwargs.pop('params')
1)覆盖
__init__
以添加self.params = kwargs.pop('params')
2) create method to access to kwargs specific key params using return self.kwargs['params']
2)使用
return self.kwargs['params']
创建用于访问kwargs特定键参数的方法
3) Mixin solution (probably badly implemented but I thing there are constraints in these two CBVs) 3)Mixin解决方案(可能执行不佳,但我认为这两个CBV中存在约束)
Ultimately, suing method 1 and 2, I manage to access merely to a string representation of the params dictionary through reversing to download/url/. 最终,使用方法1和2,我设法通过反转到download / url /来仅访问params字典的字符串表示形式。 I need a more programatically solution to obtain the original dict object.
我需要一个更具编程性的解决方案来获取原始的dict对象。 TLDR: How can I pass complex objects between views.
TLDR:如何在视图之间传递复杂的对象。
Best regards, 最好的祝福,
As suggested by kungphu, probably the best way to share complex objects between views is using the view session. 正如kungphu所建议的那样,在视图之间共享复杂对象的最佳方法可能是使用视图会话。 For future reference, I end up doing the following:
为了将来参考,我最终做了以下工作:
def done(self,form_list,**kwargs):
params={}
for form in form_list:
params.update(form.cleaned_data)
self.request.session['params']=params
return HttpResponseRedirect(reverse(self.download_url_name))
and I also changed the session serializer to pickle. 并且我还将会话序列化程序更改为pickle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.