Unable to display two forms in a view

8 Aufrufe
Direkt zur ersten ungelesenen Nachricht

chevalier

ungelesen,
24.08.2019, 11:37:1024.08.19
an Django users

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) 

Gil Obradors

ungelesen,
24.08.2019, 12:24:0324.08.19
an django...@googlegroups.com
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:
--
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...@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.

chevalier

ungelesen,
24.08.2019, 12:48:5424.08.19
an Django users
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
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Gil Obradors

ungelesen,
24.08.2019, 13:23:5624.08.19
an django...@googlegroups.com
Models and forms ?
And/Or the output of print(request.POST) ?

Where you code take you?
  • "Data in fields is incorrect, please try again"?
  • Exceptions?
  • Or  maybe to payment_form.is_valid()  as false?







Missatge de chevalier <cheva...@gmail.com> del dia ds., 24 d’ag. 2019 a les 18:49:
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@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.
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten