[英]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.