编辑Django Formwizard:获取初始数据 [英] Edit Django Formwizard: Getting Initial Data

查看:97
本文介绍了编辑Django Formwizard:获取初始数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用我的Django FormWizard编辑现有的对象。我遵循此博客文章中描述的技术,但它不工作。这是我的编辑视图:

  @login_required 
def edit_wizard(request,id):
thing = get_object_or_404(Thing,pk = id)
如果thing.user!= request.user:
raise HttpResponseForbidden()
else:
initial = {0:{'year' thing.year,
'make':thing.make,
'series':thing.series,
....等等。
},
1:{'condition':thing.condition,
....等等。
},
}

form = CreateWizard.as_view([StepOneForm,StepTwoForm,StepThreeForm],initial_dict = initial)
return form(context = RequestContext(request) ,请求=请求)

你能帮助我找出如何向向导提供初始数据我可以允许用户编辑他们的对象?感谢您的想法!






编辑:(2/18/13)



得到一个:

  / edit / 10 / __init __()中的TypeError只需一个参数(给定3个)

这是由@ sneawo的答案解决的,但是还没有传递初始数据,向导而是创建新对象。






编辑:(2/19/13)



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, form_list,** kwargs):
instance = Thing()
form_list中的表单:
for field,value in form.cleaned_data.iteritems():
setattr(instance,字段,值)
instance.user = self.request.user
instance.save()
返回render_to_response('wizard-done.html',{
'form_data': [f]的form.cleaned_data orm in form_list],
})


解决方案

根据文档,对于Django 1.4+,您可以在 initial_dict 关键字参数中传递初始数据。对于以前的版本(1.3,似乎在1.3之前不存在),关键字参数是初始。此外,您的初始数据dict中的步骤的键应该是字符串不是整数。

  initial = {'0':{' year':thing.year,
'make':thing.make,
'series':thing.series,
....等等。
},
'1':{'condition':thing.condition,
....等等。
},
}

更新
要更新同一对象,您还必须设置该id,否则django将无法知道要更新的对象。一个简单的方法是将id传递给隐藏的字段,但是您必须在(完成)方法中重新执行用户权限。

  initial = {0:{'id':thing.id,


class CreateWizard(SessionWizardView):
file_storage = FileSystemStorage(location = os .path.join(settings.MEDIA_ROOT))
def done(self,form_list,** kwargs):
id = form_list [0] .cleaned_data ['id']
thing = get_object_or_404 (Thing,pk = id)
如果thing.user!= self.request.user:
raise HttpResponseForbidden()
else:
instance = Thing()
for form in form_list:
for field,value in form.cleaned_data.iteritems():
setattr(instance,field,value)
instance.user = self.request.user
instance.save()
return render_to_response('wizard-done.html',{
'form_data':[form.cleaned_data for form在form_list],})

当然,StepOneForm应该具有隐藏字段的id:

  class StepOneForm(forms.Form):
id = forms.IntegerField(widget = forms.HiddenInput)


I am trying to edit existing objects using my Django FormWizard. I am following the technique described in this blog post, but it does not work. Here is my edit view:

@login_required
def edit_wizard(request, id):
    thing = get_object_or_404(Thing, pk=id)
    if thing.user != request.user:
        raise HttpResponseForbidden()
    else:
        initial = {0: {'year': thing.year,
                       'make': thing.make,
                       'series': thing.series,
                        ....etc.
                       },
                   1: {'condition': thing.condition,
                        ....etc.
                       },
                   }

     form = CreateWizard.as_view([StepOneForm, StepTwoForm, StepThreeForm], initial_dict=initial)
     return form(context=RequestContext(request), request=request)

Can you help me figure out how to provide the initial data to the Wizard so that I can allow users to edit their objects? Thanks for your ideas!


EDIT: (2/18/13)

Was getting a:

TypeError at /edit/10/   __init__() takes exactly 1 argument (3 given)

This was solved by @sneawo's answer below, but still no initial data is passed, and the wizard instead creates new objects.


EDIT: (2/19/13)

class CreateWizard(SessionWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT))
    def done(self, form_list, **kwargs):
        instance = Thing()
        for form in form_list:
            for field, value in form.cleaned_data.iteritems():
                setattr(instance, field, value)
        instance.user = self.request.user
        instance.save()
        return render_to_response('wizard-done.html', {
            'form_data': [form.cleaned_data for form in form_list],
        })

解决方案

As per the documentation, for Django 1.4+ you pass the initial data in initial_dict keyword argument. For previous versions(1.3, it seems it wasn't there before 1.3) the keyword argument was initial. Also, the keys for steps in your initial data dict should be strings not integers.

initial = {'0': {'year': thing.year,
                 'make': thing.make,
                 'series': thing.series,
                  ....etc.
                },
           '1': {'condition': thing.condition,
                  ....etc.
                },
          }

UPDATE: To update the same object you have to set the id also, otherwise there is no way for django to know which object to update. A simple way to do it is to pass the id in a hidden field, but you have to do the user permission check again in your (done) method.

initial = {0: {'id': thing.id,


class CreateWizard(SessionWizardView):
    file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT))
    def done(self, form_list, **kwargs):
       id = form_list[0].cleaned_data['id']
       thing = get_object_or_404(Thing, pk=id)
       if thing.user != self.request.user:
           raise HttpResponseForbidden()
       else:
         instance = Thing()
         for form in form_list:
             for field, value in form.cleaned_data.iteritems():
                 setattr(instance, field, value)
         instance.user = self.request.user
         instance.save()
         return render_to_response('wizard-done.html', {
             'form_data': [form.cleaned_data for form in form_list],})

and of course StepOneForm should have id with hidden field:

class StepOneForm(forms.Form):
    id = forms.IntegerField(widget=forms.HiddenInput)

这篇关于编辑Django Formwizard:获取初始数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆