简体   繁体   English

Django,DataWizard,SessionWizardView获取当前用户

[英]Django, DataWizard, SessionWizardView get current user

I am creating a series of forms, processed via Django's form wizard, however, the forms consist of many dropdown boxes, the content of which depend on the current user and therefore, I need to pass the User as a kewword argument. 我正在创建一系列通过Django的表单向导处理的表单,但是,表单包含许多下拉框,下拉框的内容取决于当前用户,因此,我需要将User作为kewword参数传递。

What I have at the moment is this: 我现在所拥有的是:

class ViewDataWizard(SessionWizardView):
    template_name="wizards/data/view_data.html"
    def done(self, form_list,**kwargs):
        form_data = process_form_data(form_list)
        return render_to_response('wizards/data/view_data_done.html',{'form_data':form_data})

However, I am seeking to produce this: 但是,我正在寻求产生这种结果:

class ViewDataWizard(SessionWizardView):
    template_name="wizards/data/view_data.html"
    def get_form_kwargs(self, step):
        if step == 0:
            return {'user':<USEROBJECT>}
        else:
            return {}
    def done(self, form_list,**kwargs):
        form_data = process_form_data(form_list)
        return render_to_response('wizards/data/view_data_done.html',{'form_data':form_data})

Where in the second example above, I need to substitute USEROBJECT with the current user, to seed the series of forms. 在上面的第二个示例中,我需要用当前用户替换USEROBJECT,以播种一系列表单。

Am I missing something really obvious? 我是否真的缺少明显的东西? Traditionally I would get user from request.user in a given view, however, this seems elusive in the forms wizard process... 传统上,我会在给定视图中从request.user获取用户,但是,这在表单向导过程中似乎难以捉摸...

You can get the request object from self . 您可以从self获得request对象。

def get_form_kwargs(self, step):
    if step == 0:
        return {'user': self.request.user}
    else:
        return {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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