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.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0dfb50c3-c024-4b7a-975f-d46f6ed0da09%40googlegroups.com.

Reply via email to