def courses_index():

    search = CourseSearchForm(request.form)

    if request.method == 'POST':
        return search_results(search)
    return render_template("courses/list.html", courses = Course.query.all(), form=search, find_location=Course.find_location_name())
def search_results(search):
    results = []
    search_string = search.data['search']

    if search_string:
        if search.data['select'] == 'Course name':
            query = db.session().query(Course).filter(Course.name.contains(search_string))
            results = query.all()
        elif search.data['select'] == 'Course code':
            query = db.session().query(Course).filter(Course.coursecode.contains(search_string))
            results = query.all()
        elif search.data['select'] == 'Language':
            query = db.session().query(Course).filter(Course.language.contains(search_string))
            results = query.all()
        elif search.data['select'] == 'Level':
            query = db.session().query(Course).filter(Course.level.contains(search_string))
            results = query.all()
    if not results:
        flash('No results')
        return redirect(url_for("courses_index"))
    else:
        return render_template("courses/list.html", courses = results, form=search, find_location=Course.find_location_name())