Passing data through from form submission depending on value

FrostyDuke

I am trying to pass data through from a form depending on different calculations from the form submission

Full Model

    from django.db import models
from datetime import datetime, date

# Create your models here.

year_choice = [
    ('year1','1-Year'),
    ('year3','3-Year')
]
weeksinyear = 52
hours = 6.5

class AdminData(models.Model):
    year1 = models.IntegerField()
    year3 = models.IntegerField()

    @property
    def day_rate_year1(self):
        return self.year1 / weeksinyear / hours

    @property
    def day_rate_year3(self):
        return self.year3 / weeksinyear / hours


class Price(models.Model):
        name = models.CharField(max_length=100)
        contract = models.CharField(max_length=5, choices=year_choice)
        start_date = models.DateField(default=datetime.now)
        end_date = models.DateField(default=datetime(2021,3,31))
        epoch_year = date.today().year
        year_start = date(epoch_year, 1, 4)
        year_end = date(epoch_year, 3 , 31)


        def __str__(self):
            return self.name

        @property
        def pricing(self):
            price_days = self.start_date - self.year_end
            return price_days

Views

def price_detail(request):

    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = PriceForm(request.POST)

        # check whether it's valid:
        if form.is_valid():
            form.save()

            return render(request,'confirmation.html',{'form_data': form.cleaned_data})
    else:
        form = PriceForm()


    return render(request, 'main.html', {'form': form})

So the idea is to change show details on the confirmation.html page depending on the contract type and start date, a basic but most like incorrect example is below:

def pricingcalc(request):
    if PriceForm.contract == 'year1'
        return Price.pricing * AdminData.day_rate_year1
    else
        return Price.pricing * AdminData.day_rate_year3

To summarise:

  • List output of values on confirmation page
  • Output based on number of days x price depending on a one year contract or a three year contract.

Hope this makes sense.

Thanks for the help.

Alex

If we look in documentation at how Model Forms work (more precisely here) we can see that once we call .save() on a ModelForm we'll get back an instance of that model.

In your case we can assume the following:

if form.is_valid():
    price_instance = form.save()

Now, if you want to display some dynamic computed values for your pricing model, you can add a method on your Price model that calculates them based on Price.contract.

The important idea here is that once a ModelForm is deemed valid and you call .save() on it, this will commit by default data in your database and return an instance of that model that you can use afterwards.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How Do You Redirect To Varying URLS From A Form Submission Depending On The Radio Value Selected

Getting Null Value from Form Submission

Render Data From Form on Submission in React

Single form submission of data from multiple forms

Form POST data not passing through to PHP

Passing data from form to fieldset

Passing data from the UserControl to the Form

Form Submission through Ajax and PHP

No data binding on form submission

django form submission error (passing parameter from view.py to form)

Passing data from spinner to TextView depending on user selection

Passing through data from 2 mongodb objects

Passing data from modal through multiple widgets

Passing value along with form in POST data in Django

Passing a value of a textbox from View to a method/action in Controller and then data from response back to form

How to automatically calculate formulas from new form submission data?

Form submission through ajax and reply from servlet through JSON without page refresh

Passing a Varible through a form

Passing value from for loop in ajax, into form field

Passing value from wpf form to winform

Passing the value to form from View - Django

passing Javascript variable to PHP on form submission

PHP Session not passing multiple post form data through multiple pages

Passing data from Angular form to PHP

Passing data from parent to child (Nested Form)

Passing data from multiple userControls to a form

Passing data from an html form into a Javascript object

Passing data from django form to ListView

Passing data from form to controller Laravel 5