【发布时间】:2019-09-29 14:12:45
【问题描述】:
很抱歉提出这样一个新手问题,但我被困住了,我正在寻求帮助。我使用 FormWizard 从用户那里获取订阅数据。效果很好。现在我希望他们能够使用相同的 FormWizard 更新他们的订阅。
我可以显示他们以前的输入,但是,当我真正知道要更新哪条记录时,这就是我遇到问题的地方。我能够从该路径的视图函数中的 URL 中获取 id,但我无法将 id 获取到其他视图。
我的代码如下。我被困在第 9.3 节。我不确定如何获取记录id,以便它可以更新正确的记录。如果有更好的方法,请随时提出建议并提前致谢。
urls.py
path('subscription/update/<int:id>/', service_views.wizard_edit, name='wizard-edit'),
views.py
## 9.1 Displaying the data in the form
def wizard_edit(request, id):
## Collecting the data
sub = Subscribers.objects.get(id=id)
## Displaying the data in the appropriate forms
initial = {
'0': {'industry_search':sub.industry},
'1': {'city':sub.city, 'kilometers':sub.kilometers, 'street_1':sub.street_1, 'street_2':sub.street_2},
'2': {'email':sub.email}
}
wiz = ContactWizardUpdate.as_view([ContactForm1, ContactForm2, ContactForm3], initial_dict=initial)
return wiz(request)
## 9.2 FormWizard
class ContactWizardUpdate(SessionWizardView):
template_name = 'service/subscribe.html'
form_list = [ContactForm1, ContactForm2, ContactForm3]
def done(self, form_list, **kwargs):
## Function to update the DB and save data
update_the_form_data(self, form_list)
return render(self.request, 'service/done.html')
## 9.3 Updating the database with the changes
def update_the_form_data(self, form_list):
form_data = [form.cleaned_data for form in form_list]
## Get the correct record for the update
foo = get_object_or_404(Subscribers, id=[THE ID FOR THE RECORD])
## Additional code
foo.save()
【问题讨论】:
标签: python django django-views django-formwizard