Flask app search bar
<p>I am trying to implement a search bar using Flask, but when I enter the url/search, I got a 405 error, <strong>Method Not Allowed</strong>. </p>

<p>Here is a snippet of my code. Any help would be appreciated!</p>

<p><strong>forms.py</strong></p>

<pre><code>from wtforms import StringField
from wtforms.validators import DataRequired

class SearchForm(Form):
  search = StringField('search', [DataRequired()])
  submit = SubmitField('Search',
                       render_kw={'class': 'btn btn-success btn-block'})
</code></pre>

<p><strong>views.py</strong></p>

<pre><code>from flask_login import login_required
from forms import SearchForm
from models import User

@app.route('/')
def index():
  if current_user.is_authenticated:
    return redirect(url_for('profile'))
  return render_template('index.html')

@app.route('/profile', methods=['GET', 'POST'])
@login_required
def profile():
  # some code to display user profile page

@app.route('/search', methods=['POST'])
@login_required
def search():
  form = SearchForm()
  if not form.validate_on_submit():
    return redirect(url_for('index'))
  return redirect((url_for('search_results', query=form.search.data)))

@app.route('/search_results/&lt;query&gt;')
@login_required
def search_results(query):
  results = User.query.whoosh_search(query).all()
  return render_template('search_results.html', query=query, results=results)
</code></pre>

<p><strong>models.py</strong></p>

<pre><code>from flask_sqlalchemy import SQLAlchemy
from flask_whooshalchemy import whoosh_index
from app import app

db = SQLAlchemy()

class User(db.model):
  __searchable__ = ['name']
  id = db.Column(db.Integer, primary_key=True)
  name = db.Column(db.String(64))

whoosh_index(app, User)
</code></pre>

<p><strong>search.html</strong></p>

<pre><code>{% extends 'layouts/base.html' %}
{% set page_title = 'Search' %}
{% block body %}
    &lt;div&gt;
        {{ render_form(url_for('search'), form) }} # note: render_form is some marco from another .html file
    &lt;/div&gt;
{% endblock %}
</code></pre>

<p>Because when you load page manually you using <code>GET</code> method, but only <code>POST</code> is allowed for <code>search</code> controller. You need to change</p>

<pre><code>@app.route('/search', methods=['POST'])
</code></pre>

<p>to </p>

<pre><code>@app.route('/search', methods=['GET', 'POST'])
</code></pre>

<p><strong>UPDATE</strong></p>

<p>So basically it's better to change your <code>search</code> controller. Because it's not using <em>search.html</em> and works wrong.</p>

<pre><code>@app.route('/search', methods=['GET', 'POST'])
@login_required
def search():
    form = SearchForm()
    if request.method == 'POST' and form.validate_on_submit():
        return redirect((url_for('search_results', query=form.search.data)))  # or what you want
    return render_template('search.html', form=form)
</code></pre>

<p>Also make indentation 4 spaces, as it said in <a href="https://www.python.org/dev/peps/pep-0008/#indentation" rel="nofollow">PEP-8</a></p>

python search flask full-text-search whoosh