【问题标题】:How can I pass an ID across function views and CBV? - Django如何跨函数视图和 CBV 传递 ID? - 姜戈
【发布时间】: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


    【解决方案1】:

    我想出了一个办法。我会分享以防这对其他人有帮助。

    而不是在 urls.py 文件中包含路径: path('subscription/update/&lt;int:id&gt;/', service_views.wizard_edit, name='wizard-edit'),

    我将其更改为path('subscription/update/', service_views.wizard_edit, name='wizard-edit'),,并在用户的订阅摘要页面上添加了一个编辑按钮,路径如下:&lt;a href="DOMAIN.COM/subscription/update/?id={{ detail.id }}"&gt;Edit Subscription&lt;/a&gt;

    这里是对 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()
    

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 2017-12-13
      • 1970-01-01
      • 2017-08-31
      • 2021-07-28
      • 1970-01-01
      • 2020-08-17
      • 2021-03-05
      • 2015-11-05
      相关资源
      最近更新 更多