title: WTForms Flask ValueError: View function did not return a response
body: <p>I keep getting this error and I can't figure out what the problem is -- specifically, why the form is not validating on submit. All my other forms work fine. Any help is appreciated. </p>
  
  <pre><code>Error:
  2014-02-28 15:21:26,581 :Exception on /add_item [POST]
  Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
      response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1361, in full_dispatch_request
  response = self.make_response(rv)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1439, in make_response
  raise ValueError('View function did not return a response')
  ValueError: View function did not return a response
  </code></pre>
  
  <p>Here is my model.py:</p>
  
  <pre><code>class Track(db.Model):
      __tablename__ = 'track'
      id = db.Column(db.Integer, primary_key=True)
      date = db.Column(db.Date)
      station_start = db.Column(db.Float)
      station_end = db.Column(db.Float)
      quantity = db.Column(db.Float)
      img = db.Column(db.String(200))
      caption = db.Column(db.String(600))
      area_id = db.Column(db.Integer, db.ForeignKey('area.id'))
      location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
      shift_id = db.Column(db.Integer, db.ForeignKey('shift.id'))
      material_id = db.Column(db.Integer, db.ForeignKey('material.id'))
  
      def __init__(self, date, station_start, station_end, quantity, img, caption):
          self.date = date
          self.station_start = station_start
          self.station_end = station_end
          self.quantity = quantity
          self.img = img
          self.caption = caption
  
      def get_id(self):
          return unicode(self.id)
  </code></pre>
  
  <p>The form definition from forms.py:</p>
  
  <pre><code>class TrackingForm(Form):
      date = DateField('Date (MM/DD/YYYY)', validators=[Required()], format='%m/%d/%Y')
      station_start = FloatField('Starting Station (XX.XX)')
      station_end = FloatField('Ending Station (XX.XX)')
      quantity = FloatField('Quantity')
      img = FileField('Image')
      caption = TextAreaField('Description')
      area = QuerySelectField(query_factory=getAllAreas, get_label='area')
      location = QuerySelectField(query_factory=getAllLocations, get_label='location')
      shift = QuerySelectField(query_factory=getAllShifts, get_label='shift')
      material = QuerySelectField(query_factory=getAllMaterials, get_label='material')
  </code></pre>
  
  <p>My views.py:</p>
  
  <pre><code>@app.route("/add_item", methods=["POST"])
  @login_required
  def add_item():
      form = TrackingForm()
      if form.validate_on_submit():
          img_file = form.img.data
          if img_file and allowed_file(img_file.filename):
              img_filename = secure_filename(str(uuid.uuid4()) + img_file.filename)
              img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], img_filename))
          else:
              img_filename = ""
  
          t = Track(date = form.date.data,
          station_start = form.station_start.data,
          station_end = form.station_end.data,
          quantity = form.quanitity.data,
          img = img_filename,
          caption = form.caption.data)
  
          db.session.add(t)
          db.session.commit()
  
          form_location = form.location.data
          l = Location.query.filter_by(location = form_location.location).first()
          l.tracks.append(t)
          db.session.commit()
  
          form_area = form.area.data
          a = Area.query.filter(Area.area == form_area.area).first()
          a.tracks.append(t)
          db.session.commit()
  
          form_material = form.material.data
          m = Material.query.filter_by(material = form_material.material).first()
          m.tracks.append(t)
          db.session.commit()
  
          return redirect(url_for('daily_report'))
      return redirect(url_for('daily_report'))
  </code></pre>
  
  <p>And the HTML:</p>
  
  <pre><code>&lt;div class="modal fade" id="add_item_form"&gt;
  &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
          &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;
              &lt;h4 class="modal-title"&gt;Add to Report&lt;/h4&gt;
          &lt;/div&gt;
          &lt;div class="modal-body"&gt;
              &lt;div class="container"&gt;
              &lt;div class="col-lg-3"&gt;
              &lt;form action="/add_item" method="POST" enctype="multipart/form-data" class="form-default"&gt;
              {{ form.hidden_tag() }}
              {% call macro.form_field(form.date) %} {% endcall %}
              &lt;div class="input-group input-group"&gt;
              &lt;span class="input-group-addon"&gt;Area&lt;/span&gt;
              {% call macro.form_field(form.area) %} {% endcall %}
              {% call macro.form_field(form.location) %} {% endcall %}
              &lt;/div&gt;
              &lt;p&gt;
              &lt;div class="input-group input-group"&gt;
              &lt;span class="input-group-addon"&gt;Material: &lt;/span&gt;
              {% call macro.form_field(form.material) %} {% endcall %}
              &lt;/div&gt;
              &lt;/p&gt;
              {% call macro.form_field(form.station_start) %} {% endcall %}
              {% call macro.form_field(form.station_end) %} {% endcall %}
              {% call macro.form_field(form.quantity) %} {% endcall %}
              {% call macro.form_field(form.img) %} {% endcall %}
              {% call macro.form_field(form.caption) %} {% endcall %}
              &lt;input type="submit" value="Submit" class="btn btn-primary"&gt;
              &lt;/form&gt;
              &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>