我正在为我的培训中心开发Web应用程序。
我有一个包含客户列表的页面,并且希望能够单击链接来更新其个人资料。单击“更新”后,我应该能够使用单个提交按钮在单个页面中编辑用户名,名字,...电子邮件,电话号码,街道等。我使用五种形式来完成此任务,一种用于用户,一种用于其他信息,一种用于地址,一种用于邮政编码,一种用于街道编号。 ListView和CreateView可以完美地与这五种形式一起使用,但不适用于UpdateView。我无法更新实例,并且每次提交时都会创建一个新实例。
问题是:请问我做错了什么?
谢谢。
views.py:
class ClientUpdate(UpdateView):
model = Client
template_name = 'garage/client_update.html'
form_class = ZipCodeForm
second_form = CityForm
third_form = AddressForm
fourth_form = DonneesPersonnellesForm
fifth_form = ClientForm
# success_url = reverse_lazy('garage/clients')
def get_context_data(self, **kwargs):
client = self.object
address = client.adresse
zipCode = address.zipCode
city = address.city
donneesPersonnelles = client.donnees_personnelles_client
context = super(ClientUpdate, self).get_context_data(**kwargs)
if 'form' not in context:
context['form'] = self.form_class(instance=zipCode)
if 'form2' not in context:
context['form2'] = self.second_form(instance=city)
if 'form3' not in context:
context['form3'] = self.third_form(instance=address)
if 'form4' not in context:
context['form4'] = self.fourth_form(instance=donneesPersonnelles)
if 'form5' not in context:
context['form5'] = self.fifth_form(instance=client)
return context
def get(self, request, *args, **kwargs):
client = Client.objects.get(pk=self.kwargs['pk'])
address = client.adresse
zipCode = address.zipCode
city = address.city
donneesPersonnelles = client.donnees_personnelles_client
super(ClientUpdate, self).get(request, *args, **kwargs)
form = self.form_class(instance=zipCode)
form2 = self.second_form(instance=city)
form3 = self.third_form(instance=address)
form4 = self.fourth_form(instance=donneesPersonnelles)
form5 = self.fifth_form(instance=client)
return self.render_to_response(self.get_context_data(object=self.object, form=form, form2=form2, form3=form3, form4=form4, form5=form5))
def post (self, request, *args, **kwargs):
client = Client.objects.get(pk=self.kwargs['pk'])
address = client.adresse
zipCode = address.zipCode
city = address.city
donneesPersonnelles = client.donnees_personnelles_client
self.object = self.get_object()
form = self.form_class(request.POST, instance=zipCode)
form2 = self.second_form(request.POST, instance=city)
form3 = self.third_form(request.POST, instance=address)
form4 = self.fourth_form(request.POST, instance=donneesPersonnelles)
form5 = self.fifth_form(request.POST, instance=client)
if form.is_valid() and form2.is_valid() and form3.is_valid() and form4.is_valid() and form5.is_valid():
zipCodeData = form.save(commit=False)
cityData = form2.save(commit=False)
addressData = form3.save(commit=False)
donneesPersosData = form4.save(commit=False)
clientdata = form5.save(commit=False)
zipCodeData.save()
cityData.save()
addressData.save()
donneesPersosData.save()
clientdata.save()
messages.success(self.request, 'données sauvegardées avec succes')
return HttpResponseRedirect(self.get_success_url())
else:
return self.render_to_response(
self.get_context_data(form=form, form2=form2, form3=form3, form4=form4, form5=form5))
def get_success_url(self):
return reverse('garage/clients')
找到解决方案:
class ClientUpdate(UpdateView):
template_name = 'garage/client_update.html'
success_message = "Données mises à jour avec succès"
def get(self, request, *args, **kwargs):
client = Client.objects.get(pk=self.kwargs['pk'])
address = client.adresse
zipCode = address.zipCode
city = address.city
donneesPersonnelles = client.donnees_personnelles_client
form = ZipCodeForm(instance=zipCode)
form2 = CityForm(instance=city)
form3 = AddressForm(instance=address)
form4 = DonneesPersonnellesForm(instance=donneesPersonnelles)
form5 = ClientForm(instance=client)
context = {'form': form, 'form2': form2, 'form3': form3, 'form4': form4, 'form5': form5, }
return render(request, self.template_name, context)
def post (self, request, *args, **kwargs):
client = Client.objects.get(pk=self.kwargs['pk'])
address = client.adresse
zipCode = address.zipCode
city = address.city
donneesPersonnelles = client.donnees_personnelles_client
form = ZipCodeForm(request.POST, instance=zipCode)
form2 = CityForm(request.POST, instance=city)
form3 = AddressForm(request.POST, instance=address)
form4 = DonneesPersonnellesForm(request.POST, instance=donneesPersonnelles)
form5 = ClientForm(request.POST, instance=client)
if form.is_valid() and form2.is_valid() and form3.is_valid() and form4.is_valid() and form5.is_valid():
zipCodeData = form.save(commit=False)
cityData = form2.save(commit=False)
addressData = form3.save(commit=False)
donneesPersosData = form4.save(commit=False)
clientData = form5.save(commit=False)
cityData.save()
addressData.city = cityData
zipCodeData.save()
addressData.zipCode = zipCodeData
addressData.save()
clientData.adresse = addressData
donneesPersosData.save()
clientData.donnees_personnelles_client = donneesPersosData
clientData.save()
return redirect('garage:clients')
context = {'form': form, 'form2': form2, 'form3': form3, 'form4': form4, 'form5': form5, }
return render(request, self.template_name, context)
def get_context_data(self, **kwargs):
client = Client.objects.filter(pk=self.kwargs['pk'])
address = client.adresse
zipCode = address.zipCode
city = address.city
donneesPersonnelles = client.donnees_personnelles_client
context = super(ClientUpdate, self).get_context_data(**kwargs)
if 'form' not in context:
context['form'] = self.form_class(instance=zipCode)
if 'form2' not in context:
context['form2'] = self.second_form(instance=city)
if 'form3' not in context:
context['form3'] = self.third_form(instance=address)
if 'form4' not in context:
context['form4'] = self.fourth_form(instance=donneesPersonnelles)
if 'form5' not in context:
context['form5'] = self.fifth_form(instance=client)
return context