简体   繁体   中英

Complex objects passed as args/kwargs between class based views in Django

I currently have a SessionWizardView from formtools package implemented.

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:

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

2) create method to access to kwargs specific key params using return self.kwargs['params']

3) Mixin solution (probably badly implemented but I thing there are constraints in these two CBVs)

Ultimately, suing method 1 and 2, I manage to access merely to a string representation of the params dictionary through reversing to download/url/. I need a more programatically solution to obtain the original dict object. TLDR: How can I pass complex objects between views.

Best regards,

As suggested by kungphu, probably the best way to share complex objects between views is using the view session. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM