[英]How can I pass an ID across function views and CBV? - Django
对于这样一个菜鸟问题,我很抱歉,但我被困住了,我正在伸出援手。 我使用 FormWizard 从用户那里获取订阅数据。 效果很好。 现在我希望他们能够使用相同的 FormWizard 更新他们的订阅。
我可以展示他们以前的输入,但是,当涉及到实际知道要更新哪条记录时,这就是我遇到麻烦的地方。 我能够从该路径的视图 function 中的 URL 获取id
,但我无法将id
获取到其他视图。
我的代码如下。 我被困在第 9.3 节。 我不确定如何获取记录id
,以便它可以更新正确的记录。 如果有更好的方法,请随时提出建议并提前致谢。
网址.py
path('subscription/update/<int:id>/', service_views.wizard_edit, name='wizard-edit'),
视图.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()
我想出了一个办法。 我会分享以防这对其他人有帮助。
而不是 urls.py 文件中的路径: path('subscription/update/<int:id>/', service_views.wizard_edit, name='wizard-edit'),
我将其更改为path('subscription/update/', service_views.wizard_edit, name='wizard-edit'),
并在用户的订阅摘要页面上添加了一个编辑按钮,路径如下, <a href="DOMAIN.COM/subscription/update/?id={{ detail.id }}">Edit Subscription</a>
以下是对 views.py 文件的编辑:
## 9.1 Displaying the data in the form
def wizard_edit(request): ## NEW
id_ = request.GET.get('id') ## NEW
## Collecting the data
sub = Subscribers.objects.get(id=id_) ## NEW
## 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]
id_ = self.request.GET.get('id') ## NEW
## Get the correct record for the update
foo = get_object_or_404(Subscribers, id=id_) ## NEW
## Additional code
foo.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.