Saturday, August 24, 2019

Re: Unable to display two forms in a view

Even after adding the instance i am unable to populate my form. However, if i individually populate each form both of them populate.

def updatePayment(request, id):
sim = get_object_or_404(Sim, pk=id)
payment = AddPaymentForm(request.POST or None, instance=Payment)
if request.method == "POST":
payment_form = AddPaymentForm(request.POST or None, instance=Payment)
sim_form = UpdatePayment(request.POST, instance=sim)
try:
if payment_form.is_valid():
payment_form.save()
sim_form.save()
messages.success(request, ("Payment has been updated"))
else:
messages.warning(request, ("Data in fields is incorrect, please try again"))
except Exception as e:
messages.warning(request, ("Error: {}".format(e)))
else:
form = UpdatePayment(instance=sim)
payment = AddPaymentForm(request.POST or None, instance=Payment)
context = {'payment': payment, 'form': form,}
return render(request, 'payment/updatePayment.html', context)

Thanks for you help

On Saturday, August 24, 2019 at 9:24:03 AM UTC-7, Gil Obradors wrote:
Of course, you don't have poppulated payment_form ? Only initializated,

payment_form = AddPaymentForm()

If I'm not helping you, please attach more code


good luck!

Missatge de chevalier <cheva...@gmail.com> del dia ds., 24 d'ag. 2019 a les 17:37:

I have two related models (Sim and Payment - A Sim can have many Payment). On searching of Sim (by ID ) i want its detail to be populated in a form (which i am able to). But using the same form i want to save the details of Payment model as well and want to update the Sim model as well.

I am only able to populate only Sim form using the id but the AddPaymentForm is not populating.


def updatePayment(request, id):      sim  = get_object_or_404(Sim, pk=id)      payment = AddPaymentForm()      sim_form = UpdatePayment(request.POST, instance=sim)      if request.method == "POST":          # sim_form = UpdatePayment(request.POST, instance=payment)          payment_form = AddPaymentForm()          try:              if payment_form.is_valid():                  payment_form.save()                  sim_form.save()                  messages.success(request, ("Payment has been updated"))              else:                  messages.warning(request, ("Data in fields is incorrect, please try again"))          except Exception as e:              messages.warning(request, ("Error: {}".format(e)))      else:          form = UpdatePayment(instance=sim)          payment = AddPaymentForm()      context = {'payment': payment, 'form': form,}      return render(request, 'payment/updatePayment.html', context) 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5a7edbc8-7d20-42aa-8d8e-e8c122026a12%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d6e0b8b8-437a-468f-a7ec-bcd1e34fa4e5%40googlegroups.com.

No comments:

Post a Comment