0

I am trying to generate formset which would look like this. formset output needed and then access formset data to put it into pandas dataframe for calculation that could be accessible in another python .py file. However, my code does not fetch any user input to me once I test it. Please advise what should be done to fix it?

views.py

   from django.shortcuts import render
   from django.http import HttpResponse
   from django.http import HttpResponseRedirect
   from django.shortcuts import render
   from .forms import AssumptionsForm, AssumptionsFormSet
   from django.forms import formset_factory

    data_list = []        

    def index(request):
        return HttpResponse("Hello, client. You're at the inputs page.")

    def get_assumptions(request):    

        if request.method == 'POST':

            formset = AssumptionsFormSet(request.POST)

            if formset.is_valid():
                for f in formset:
                    cd = f.cleaned_data
                    data1 = cd.get('bad')
                    data2 = cd.get('likely')
                    data3 = cd.get('best')
                    data_list.append(data1)
                    data_list.append(data2)
                    data_list.append(data3)
        else:
            formset = AssumptionsFormSet()

        return render(request, 'assumptions.html', {'formset': formset})

assumptions.html

    <form action="/analysis2/" method="post">
    {% csrf_token %}
    <table>
    {% for form in formset %}
        {{ form }}
    {% endfor %}
    </table>
    <input type="submit" value="Submit" />
    </form>

forms.py

from django import forms
from django.forms import formset_factory

class AssumptionsForm(forms.Form):
    #title = forms.CharField()
    bad = forms.FloatField()
    likely = forms.FloatField()
    best = forms.FloatField()

AssumptionsFormSet = formset_factory(AssumptionsForm, extra = 5)

1 Answer 1

1

As stated in the docs in order to use the formset in your view you need to include the formset.management_form in your form. Here is the example from the docs:

<form method="post">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        {{ form }}
        {% endfor %}
    </table>
</form>

Here is a good example of how to use formsets: https://medium.com/@adandan01/django-inline-formsets-example-mybook-420cc4b6225d The pandas portion is the same as posted in your previous post on the topic from Django forms to pandas DataFrame

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This is helpful. However, I still have difficulty in creating 6x6 table I would like to have for customer to input in. Also, I would like to have many of such tables on the same page. I tried inline_formsets factories but got stuck and the only ouput I get is the same table 3x1 repeated 5 times instead of one table 6x6. I would highly appreciated any ideas on this issue as well. Thank you.
Well a formset is basically just adding rows to a table so you would have a form with six fields, your form only has three fields bad, likely and best. You could do something like bad, bad1, likely, likely1, best and best1. If you want them to have the same labels ie. bad and bad instead bad and bad1 you would have to edit the init as shown here stackoverflow.com/questions/636905/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.