我想在与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')我将能够呈现预填充如果是,请在我的模板中填写,然后我该如何退货呢?
答案 0 :(得分:0)
您可以像这样定义get_initial()函数:
def get_initial(self):
initial = {
'part_id' : self.request.user.part_id
'entry_date: self.....
}
return initial
此函数将预先填充您在上下文中传递的表单。