title: search function (query in Flask, SQLAlchemy)
body: <p>I'm new to programming and Flask and I am stuck on this problem.</p>
  
  <p>I am trying to implement a search function in a web application that will take data from a form and compare it to a value in the database and list results.</p>
  
  <p>This is what I have so far:</p>
  
  <p>views.py</p>
  
  <pre><code>@app.route('/search', methods=['GET', 'POST'])
  def search():
      searchForm = searchForm()
      courses = models.Course.query.order_by(models.Course.name).all()
      if searchForm.validate_on_submit():
          for i in courses:
              if searchForm.courseName.data == i.name:
                searchResult = models.Course.filter(Course.name.like('%searchForm.courseName.data%'))
      return render_template('courselist.html', courses = courses, searchResult = searchResult)
  </code></pre>
  
  <p>form.py</p>
  
  <pre><code>class searchForm(Form):
      courseName = StringField('Search course', validators=[DataRequired(), Length(max=60)])
  </code></pre>
  
  <p>database models.py</p>
  
  <pre><code>class Course(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(40), unique=True)
      courseCode = db.Column(db.String(10), unique=True)
      duration = db.Column(db.Integer)
      maxStudents = db.Column(db.Integer)
      startDate = db.Column(db.DateTime)
      prerequisites = db.Column(db.String(500))
      trainerID = db.Column(db.Integer, db.ForeignKey('trainer.id'))
      venueID = db.Column(db.Integer, db.ForeignKey('venue.id'))
  
      sessions = db.relationship('Session', backref='course', lazy='dynamic')
      bookings = db.relationship('Booking', backref='course', lazy='dynamic')
  </code></pre>
  
  <p>html file</p>
  
  <pre><code>{% extends "index.html" %}
  {% block content %}
  &lt;h3&gt;Courses:&lt;/h3&gt;
  &lt;ul&gt;
      {% for course in courses %}
      &lt;li&gt;
      &lt;h4&gt;&lt;a href="/viewcourse?id={{course.id}}"&gt;{{course.name}}&lt;/a&gt;
      &lt;a class="btn btn-success" href="/editcourse?id={{course.id}}"&gt;Book&lt;/a&gt;
      &lt;a class="btn btn-info" href="/editcourse?id={{course.id}}"&gt;Edit&lt;/a&gt;
      &lt;a class="btn btn-danger" href="/deletecourse?id={{course.id}}"&gt;Delete&lt;/a&gt;&lt;/h4&gt;
      &lt;/li&gt;
      {% endfor %}
  &lt;/ul&gt;
  {% endblock %}
  </code></pre>
  
  <p>I think the general logic is right but I need some help adjusting it.</p>