如何在Flask上保存变量的表单条目?

2024-06-16 11:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

关于my views.py,我有以下几点:

#defining standard parameters for students
#ba = below average, av = average, aa = above average

bast_param = [0,5] #ba prefix for below average student
avst_param = [5,7] #av prefix for average student
aast_param = [7,10] #aa prefix for above average student

min_bast = bast_param[0]
max_bast = bast_param[1]
min_avst = avst_param[0]
max_avst = avst_param[1]
min_aast = aast_param[0]
max_aast = aast_param[1]

@app.route("/parameters", methods=["GET","POST"])
def parameters():

    if request.method == "POST":
        req = request.form
        print(req)
        std_type = req.get("std_type")
        new_max_grade = req.get("new_max_grade")
        new_min_grade = req.get("new_max_grade")

        #if user select below average on form
        if std_type == "Below average":
            global min_bast
            global max_bast
            min_bast = new_min_grade
            max_bast = new_max_grade

        #if user select above average on form
        if std_type == "Above average":
            global min_bast
            global max_bast
            min_aast = new_min_grade
            max_aast = new_max_grade

        #if user select average on form
        if std_type == "Average":
            global min_bast
            global max_bast
            min_avst = new_min_grade
            max_avst = new_max_grade


        return render_template('/public/parameters.html', min_bast=min_bast, max_bast=max_bast, min_avst=min_avst,max_avst=max_avst, min_aast=min_aast, max_aast=max_aast)



    return render_template('/public/parameters.html', min_bast=min_bast, max_bast=max_bast, min_avst=min_avst,max_avst=max_avst, min_aast=min_aast, max_aast=max_aast)

html文件的参数为:


{% extends "/public/templates/public_template.html" %}

{% block  title %}Index{% endblock%}

{% block main %}
  <div class="container">
    <div class="row">
      <div class="col">
        <br />
        <h1>Parameters</h1>
        <hr/>
      </div>
    </div>
    <div class="row">
    <div class="col-sm">
      <h6>Below average students</h6>
      <label for="exampleFormControlSelect2">Min:</label> {{min_bast}} <label for="exampleFormControlSelect2">Max:</label> {{max_bast}}
      <h6>Average students</h6>
      <label for="exampleFormControlSelect2">Min:</label> {{min_avst}} <label for="exampleFormControlSelect2">Max:</label> {{max_avst}}
      <h6>Above average students</h6>
      <label for="exampleFormControlSelect2">Min:</label> {{min_aast}} <label for="exampleFormControlSelect2">Max:</label> {{max_aast}}
    </div>
    <div class="col-sm">
      <form action="/parameters" method="POST"> 
  <div class="form-group">

    <label for="exampleFormControlSelect1">Select student type</label>
    <select class="form-control" name="std_type">
      <option>Below average</option>
      <option>Average</option>
      <option>Above average</option>
      </select>
  </div>
  <div class="form-group">
    <label for="exampleFormControlSelect2">Min. Grade</label>
     <input type="number" class="form-control" name="new_min_grade" placeholder="0.0" step="0.01" min="0" max="10">
  </div>
  <div class="form-group">
    <label for="exampleFormControlSelect2">Max. Grade</label>
     <input type="number" class="form-control" name="new_max_grade" placeholder="0.0" step="0.01" min="0" max="10">
  </div>
  <button class="btn btn-primary" type="submit">Set new</button>
</form>

    </div>
  </div>

    <div class="row">
      <div class="col">


    </div>
    </div>
  </div>

{% endblock %}

在html文件的左侧,我显示views.py上的预设参数。但是我在右边设置了这个表单,以便从中获取值并更新变量(在app.route之前声明(“/parameters”在views.py上),并更新在parameters html文件左侧列中显示的值

我只是不明白是什么阻止了这一切的发生?有人能帮我吗


Tags: divformnewforparamtypeminlabel