python - 如何跨函数视图和 CBV 传递 ID?- 姜戈
问题描述
对于这样一个菜鸟问题,我很抱歉,但我被困住了,我正在伸出援手。我使用 FormWizard 从用户那里获取订阅数据。效果很好。现在我希望他们能够使用相同的 FormWizard 更新他们的订阅。
我可以展示他们以前的输入,但是,当涉及到实际知道要更新哪条记录时,这就是我遇到麻烦的地方。我能够从该路径的视图函数中的 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()
推荐阅读
- r - 如何在 map2 函数 (purrr) 中对列进行子集化,以便向量长度一致?
- c++ - 为什么 std::stable_sort() 的比较器函数的参数必须设置为常量?
- php - HTML代码转换为PHP并与html合并
- c++ - 如何将**ptr的第N个元素分配给变量?
- ffmpeg - 如何为特定(一组)文件找到最佳 FFmpeg libx265 编码设置
- android-studio - 如何在 Android Studion 中单击 Webview 应用程序上的链接后打开社交媒体应用程序
- android - TextView 文本及其可绘制到 textview 中心
- c# - NI LabVIEW NetworkVariableManager 是否应该保持连接?
- c++ - 为什么我不能在不进行任何类型转换的情况下将浮点数放入任何类型的 ptr 中?
- windows - Electron 在创建之前检查窗口是否已经打开和关闭