title: How do we handle empty forms in Flask?
body: <p>I've written an app that reads data from a csv file and displays the student id and course id and their marks. I am having difficulty in handling situations when no buttons are selected and no text written and submit is pressed or when something is written in text field and no buttons are selected and submit is pressed.
  Flask should render error.html in both of the above situations but I am getting a bad request error.</p>
  <p>Please see the below code</p>
  <pre><code>from flask import render_template
  from flask import Flask, request
  from matplotlib import pyplot as plt
  
  
  def read_csv():# Function to read csv
      student_dic = {}
      courses = []
  
      file = open(&quot;./data.csv&quot;)
      count = 0
      a = file.readlines()
      for line in a:
  
          r = line.split(&quot;,&quot;)
          if r[1] not in courses:
              courses.append(r[1].strip())
          r[2] = r[2].rstrip()
          if count == 0:
              count = count + 1
              continue
  
          if (r[0]) in student_dic:
  
              k = {}
              k[(r[1].strip())] = int(r[2])
              student_dic[(r[0])].update(k)
  
  
  
          else:
  
              student_dic[(r[0])] = {(r[1].strip()): int(r[2])}
  
      return ([student_dic, courses])
  
  
  def plot_hist(marks_list):# Function to plot
      plt.hist(marks_list)
      plt.savefig(&quot;./static/hist.png&quot;)
      plt.close()
  
  
  app = Flask(__name__)
  
  
  @app.route(&quot;/&quot;, methods=['GET', 'POST'])
  def index():
      if request.method == &quot;GET&quot;:
          return render_template(&quot;index.html&quot;)
  
      elif request.method == &quot;POST&quot;:
          option = request.form[&quot;ID&quot;]
          print(&quot;here&quot;,option)
          dic, courses = read_csv()
          if option == &quot;student_id&quot;:
              user_input = (request.form[&quot;id_value&quot;])
  
              if user_input == '':
                  return render_template(&quot;error.html&quot;)
              else:
  
                  if user_input in dic:
                      student_id_dic = dic[user_input]
                      total_marks = sum(student_id_dic.values())
  
                      return render_template(&quot;student_details.html&quot;, course_id_data=student_id_dic,
                                             student_id_data=user_input,
                                             total_marks_data=total_marks)
                  else:
                      return render_template(&quot;error.html&quot;)
  
          elif option == &quot;course_id&quot;:
              user_input_course = request.form[&quot;id_value&quot;]
              if user_input_course == '':
                  return render_template(&quot;error.html&quot;)
              else:
  
                  if user_input_course in courses:
                      ct = 0
                      max_marks = -1
                      total = 0
                      mark_list = []
                      h = 0
                      for student_id in dic:
  
                          for key, value in dic[student_id].items():
  
                              if key == user_input_course:
                                  mark_list.append(value)
                                  ct = ct + 1
                                  total = total + value
                                  if value &gt; max_marks:
                                      max_marks = value
                      plot_hist(mark_list)
                      return render_template(&quot;course_details.html&quot;, average_marks=(total / ct), maximum_marks=max_marks,
                                             img='./static/hist.png')
  
                  else:
                      return render_template(&quot;error.html&quot;)
  
          return render_template(&quot;error.html&quot;)
      return render_template(&quot;error.html&quot;)
  
  
  if __name__ == &quot;__main__&quot;:
      app.run(host='0.0.0.0')
      app.debug = True
  </code></pre>
  <p>Here is the index html file</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
  
      &lt;title&gt;Enter Details&lt;/title&gt;
   
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class=&quot;main&quot;&gt;
          &lt;h1&gt;Enter the details&lt;/h1&gt;
          &lt;form method=&quot;POST&quot; action=&quot;/&quot; id = &quot;data-form&quot;&gt;
          &lt;input type=&quot;radio&quot; name=&quot;ID&quot; value=&quot;student_id&quot; /&gt;
          &lt;label&gt;Student ID&lt;/label&gt;
          &lt;input type=&quot;radio&quot; name=&quot;ID&quot; value=&quot;course_id&quot; /&gt;
          &lt;label&gt;Course ID&lt;/label&gt;
          &lt;input type=&quot;text&quot; name=&quot;id_value&quot; /&gt;
          &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  
     
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>This is the error html</p>
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;en&quot;&gt;
  &lt;head&gt;
  
      &lt;title&gt;Something Went Wrong&lt;/title&gt;
   
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div class=&quot;main&quot;&gt;
          &lt;h1&gt;Wrong Inputs&lt;/h1&gt;
      &lt;p&gt; Something went wrong  &lt;/p&gt;
          &lt;br&gt;
  
          &lt;a href=&quot;/&quot;&gt;Go Back&lt;/a&gt;
  
      &lt;/div&gt;
  
     
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>