Radomir Mijovic
Radomir Mijovic

Reputation: 99

Django, i can't put function form inside generic.UpdateView

I am making a app for one kindergarten in my city. I have kids model and payment model. For updating kid I am using class based view generic UpdateView and for creating a payment i am using form and function view. I have not problems with payment form when I am using a different template but when I try to put it on the same template, payment form is not showing up and it's not working. Is it possible to have payment form on same template as UpdateView class ? I am using UpdateView class as profile page and I would like to have payment form on the same page. Please help. Thanks

models:

class Kids(models.Model):


    name = models.CharField(max_length=100, blank=True, null=True)
    city_birthday = models.CharField(max_length=100, blank=True, null=True)
    custom_id = models.CharField(max_length=100 ,blank=True, null=True)
    gender = models.CharField(max_length=100, choices=gender_choices, null=True, blank=True)
    address = models.CharField(max_length=250, null=True, blank=True)
    contact_phone = models.CharField(max_length=100, blank=True, null=True)
    family_size = models.IntegerField(null=True, blank=True)
    living_with = models.CharField(max_length=100, choices=living_choices, null=True, blank=True)
    number_of_preschool_kids_in_family = models.IntegerField(null=True, blank=True)
    kid_already_been_in_kindergarten = models.CharField(max_length=100, choices=preschool_choices, 
    null=True, blank=True ,default=False)

    father_name = models.CharField(max_length=100, blank=True, null=True)
    father_education_level = models.CharField(max_length=200, blank=True, null=True)
    father_company = models.CharField(max_length=200, blank=True, null=True)

    mother_name = models.CharField(max_length=100, blank=True, null=True)
    mother_education_level = models.CharField(max_length=200, blank=True, null=True)
    mother_company = models.CharField(max_length=200, blank=True, null=True)

   parent_notes = models.CharField(max_length=500, blank=True, null=True)

   program_choice = models.CharField(max_length=100, choices=kindergarten_program_choice, null=True, 
   blank=True)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name']


class Payment(models.Model):
    user = models.ForeignKey(Kids, on_delete=models.CASCADE, blank=True, null=True)
    bank_paper_id = models.IntegerField(null=True, blank=True)
    payment_date = models.CharField(max_length=100, null=True, blank=True)
    paid = models.FloatField(null=True, blank=True)
    need_to_pay = models.FloatField(null=True, blank=True)
    notes = models.CharField(max_length=500, blank=True, null=True)

    def __str__(self):
        return self.user.name

views:

class UpdateKidView(UpdateView):
    model = Kids
    fields = '__all__'
    template_name = 'vrtic/update_kid.html'
    success_url = reverse_lazy('vrtic:kids')


def create_payment(request, pk):
    kid = Kids.objects.get(id=pk)
    payment_form = PaymentForm()

    if request.method == 'POST':
        payment_form = PaymentForm(request.POST)
        if payment_form.is_valid():
            payment = payment_form.save(commit=False)
            payment.user = kid
            payment_form.save()
            return redirect('vrtic:kids')

    context = {
    'payment_form': payment_form,
    'kid': kid
    }
    return render(request, 'vrtic/update_kid.html', context)

form:

class PaymentForm(forms.ModelForm):

    class Meta:
        model = Payment
        fields = '__all__'

Upvotes: 0

Views: 51

Answers (1)

Nebojsa
Nebojsa

Reputation: 59

class UpdateKidView(UpdateView):
    model = Kids
    form_class = KidsForm
    second_form_class = PaymentForm
    template_name = 'vrtic/update_kid.html'
    success_url = reverse_lazy('vrtic:kids')

    def get_context_data(self, **kwargs):
        context = super(UpdateKidView, self).get_context_data(**kwargs)
        context['form'] = self.form_class(instance=self.get_object())
        context['second_form'] = self.second_form_class()
        return context

    def post(self, request, **kwargs):
        kids_form = self.form_class(request.POST, request.FILES, instance=self.get_object())
        if kids_form.is_valid():
              kid = kids_form.save()
              payment_form = self.second_form_class(request.POST)
              ...

Not the happiest solution, but u got the idea, if need more help contact me to explain on Serbian, not sure how are the rules here for languages : )

Upvotes: 1

Related Questions