Turn a dataframe into a form

33 views
Skip to first unread message

Charlie Mitchell

unread,
Nov 12, 2019, 5:35:59 PM11/12/19
to Django users
I am creating a form in Django. There are some static fields included but I also want to include some fields based on a csv file. However, I can't figure out how to make those text fields part of the form submission.

forms.py
class OptimizerForm(forms.Form):
    no_lineups = forms.IntegerField(label='Number of Lineups', min_value=1, initial=1)
    min_deviation = forms.DecimalField(label='Minimum Deviation %', min_value=0, initial=0)
    max_deviation = forms.DecimalField(label='Maximum Deviation %', min_value=0, initial=15)
    randomize = forms.BooleanField(label='Randomize Lineups', initial=True)

views.py
def create_optimizer(request):
   
if request.method == 'POST':
        form
= OptimizerForm(request.POST)
       
if form.is_valid():
            no_lineups
= form.cleaned_data['hjk']
           
with open('C:\\Users\\Charlie\\Desktop\\Fantasy Fire\\website\\optimizer\\lineups.csv') as myfile:
                response
= HttpResponse(myfile, content_type='text/csv')
                response
['Content-Disposition'] = 'attachment; filename=lineups.csv'
               
return response

   
else:
        form
= OptimizerForm()
        df
= Optimizer.get_daily_roster(
           
'C:\\Users\\Charlie\\Desktop\\Fantasy Fire\\website\\optimizer\\Predictions.csv')
        df
= df.drop(columns=['Name + ID', 'Game Info', 'Unnamed: 0', 'Unnamed: 0.1', 'name'])
        df
= df.rename(columns={'TeamAbbrev': 'Team', 'AvgPointsPerGame': 'Predicted FP'})
        df
['Predicted FP'] = df['Predicted FP'].apply(lambda x: round(float(x), 2))
        df
['Predicted FP'] = df['Predicted FP'].apply(
           
lambda x: "<input type='text' value=" + str(x) + " id='id_predicted_fp'>")
        df
['Min Exposure'] = "<input type='text' value=" + str(0) + ">"
        df
['Max Exposure'] = "<input type='text' value=" + str(100) + ">"
        html_table
= df.to_html(index=False, justify='left', escape=False,
                                classes
=[
                                   
'table table-bordered table-striped table-hover table-responsive table-sm, container-fluid'])
   
return render(request, 'optimizer/optimizer.html', {'form': form, 'player_table': html_table})

optimizer.html
{% extends "optimizer/base.html" %}
{% block content %}
<h1>Optimizer</h1>
<form method="post" action="/
optimizer/">
    {% csrf_token %}
    <button type="
submit" name="Generate Lineups">Generate Lineups</button>
    <!--<p>Number of Lineups: <input id="
no_lineups" type="number" name="no_lineups" value=1 min="1"></p>-->
    <!--<br>-->
    <!--<br>-->
    {{form}}
    <div class="
table-responsive">
        {{ player_table |safe }}
    </div>
</form>
{% endblock content %}

The values that are editable in the table are not being included in the cleaned_data dictionary upon submitting the form. The only fields included are the ones statically created in forms.py. I want the fields in the table to be included in the dictionary.
Reply all
Reply to author
Forward
0 new messages