【问题标题】:display the UpdateView form on the same page as CreateView form in django在 django 中与 CreateView 表单在同一页面上显示 UpdateView 表单
【发布时间】:2017-07-08 17:19:06
【问题描述】:

我想在与 CreateView 表单相同的页面上显示 UpdateView 表单,因此每当单击“编辑”按钮时,UpdateView 表单都会显示在同一页面上

<button type="button" class="edit_btn" data-url="{% url 'parts:stock_update_view' stock.id %}">Edit</button>

编辑按钮的ajax:

         $.ajax({
             url: $(this).data('url'),
             data: {            
             },
             dataType: 'json',
             success: function (data) {
             alert(data.name);
             }
         });
     }
  });
class stock_update_view(UpdateView):
    model = part_stock
    fields = ['part_id','entry_date','supplier','amount','remaining']
    success_url = reverse_lazy('parts:part_list')
    template_name = 'part_detail.html'

    def get_context_data(self, **kwargs):
        context = super(stock_update_view, self).get_context_data(**kwargs)
        context['update_form'] = context.get('form')
        return context

    def get(self, request, *args, **kwargs):
        username = request.GET.get('username', None)
        data = {

        }
        return JsonResponse(data)

我想将 UpdateView 表单作为 jsonResponse 取回,以便我可以在我的模板中呈现它。返回 context['update_form'] = context.get('form') 后,我将能够呈现预填充的如果是,请在我的模板中填写表格,那么我该如何返回?

【问题讨论】:

    标签: ajax django django-views


    【解决方案1】:

    您可以像这样定义 get_initial() 函数:

    def get_initial(self):
        initial = {
                    'part_id' : self.request.user.part_id
                    'entry_date: self.....
                  }
        return initial
    

    此函数将预先填充您在上下文中传递的表单。

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 2021-01-19
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 2015-06-23
      相关资源
      最近更新 更多