Getting Method Not Allowed error when trying to build a RESTless API
<p>I am learning <code>Flask</code> and I have found various snippets which show how to define models with <code>SQLAlchemy</code>, REST API with <code>Flask-restless</code> and forms with <code>Flask-wtf</code> (I'm not very familiar with REST API). More precisely I took inspiration from:</p>

<ul>
<li><a href="https://flask-restless.readthedocs.org/en/stable/quickstart.html" rel="nofollow noreferrer">Flask-restless Quickstart</a> (from the doc)</li>
<li><a href="http://flask.pocoo.org/snippets/60/" rel="nofollow noreferrer">Automatically create a WTForms Form from model</a> (which dates back to 2011)</li>
<li><a href="https://stackoverflow.com/questions/16406496/rendering-forms-with-flask-wtform">This SO question</a> for the basic template</li>
</ul>

<p>However I have not been able to create a fully working example. Building on the bits you can find online, I want to create a model with 2 classes <code>Person</code> and <code>Computer</code> (a <code>Person</code> can be associated with several <code>Computer</code>s) and a form to add a new <code>Person</code>. Here is the code I have assembled.</p>

<p>The layout is the following:</p>

<pre><code>test_flask/
├── test_flask.py
├── config.py
└── templates
    └── new_person.html
</code></pre>

<p>The main file <code>test_flask.py</code> contains:</p>

<pre><code>from flask import Flask, request, flash, redirect, render_template, url_for
import flask.ext.sqlalchemy
from wtforms.ext.sqlalchemy.orm import model_form
import flask.ext.restless
from flaskext.wtf import Form

# Create the Flask application and the Flask-SQLAlchemy object.
app = Flask(__name__)
app.config.from_object('config')
db = flask.ext.sqlalchemy.SQLAlchemy(app)

# Create the Flask-SQLALchemy models.
class Person(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    birth_date = db.Column(db.Date)


class Computer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode, unique=True)
    vendor = db.Column(db.Unicode)
    purchase_time = db.Column(db.DateTime)
    owner_id = db.Column(db.Integer, db.ForeignKey('person.id'))
    owner = db.relationship('Person', backref=db.backref('computers',
                                                         lazy='dynamic'))

# Create the database tables.
db.create_all()

# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)

# Create API endpoints.
manager.create_api(Person, methods=['GET', 'POST', 'DELETE'])
manager.create_api(Computer, methods=['GET'])

# Create a form class for class Person.
PersonForm = model_form(Person, base_class=Form)


@app.route('/')
def hello():
    return 'Hello World'


@app.route("/api/new_person")
def new_person():
    # The new person
    person = Person()
    # Create a form
    form = PersonForm(request.form, person)
    if form.validate_on_submit():
        form.populate_obj(person)
        person.post()
        flash("new person %s inserted updated" % person)
        return redirect(url_for("new_person"))
    return render_template("new_person.html", form=form)

if __name__ == '__main__':
    # start the flask loop
    app.run()
</code></pre>

<p>The <code>config.py</code> file contains:</p>

<pre><code>DEBUG = True
WTF_CSRF_SECRET_KEY = 'a random string'
SECRET_KEY = 'you-will-never-guess'
WTF_CSRF_ENABLED = True
SQLALCHEMY_DATABASE_URI = 'sqlite:////tmp/test.db'
</code></pre>

<p>The template <code>new_person.html</code> contains:</p>

<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta http-equiv="content-type" content="application/json"&gt;
    &lt;title&gt;New person&lt;/title&gt;
  &lt;/head&gt;
{% block body %}
    &lt;form action="new_person" method=post class=add-entry&gt;
      &lt;dl&gt;
        &lt;dt&gt;Name:
        &lt;dd&gt;{{form.name}}
        &lt;dt&gt;Birth date:
        &lt;dd&gt;{{form.birth_date}}
        &lt;dd&gt;&lt;input type=submit value=submit&gt;
      &lt;/dl&gt;
    &lt;/form&gt;
{% endblock %}
&lt;/html&gt;
</code></pre>

<p>I can see the form to add a new person at <code>http://127.0.0.1:5000/api/new_person/</code> but I got a "Method Not Allowed" error when submitting it.</p>