0

尝试将一些简单的文本插入数据库时​​,我不断收到此错误。

Method Not Allowed
The method is not allowed for the requested URL."

我正在从 PHP 迁移到 python,所以请耐心等待。

代码是:

from flask import Flask, request, session, g, redirect, url_for, \
    abort, render_template, flash

from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/pythontest'
    db = SQLAlchemy(app)
    app = Flask(__name__)

@app.route('/justadded/')
def justadded():
    cur = g.db.execute('select TerminalError, TerminalSolution from Submissions order by id desc')
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
    return render_template('view_all.html', entries=entries)

@app.route('/new', methods= "POST")
def newsolution():
    if not request.method == 'POST':
        abort(401)
    g.db.execute('INSERT INTO Submissions (TerminalError, TerminalSolution, VALUES (?, ?)'
                [request.form['TerminalError'], request.form['TerminalSolution']])
    g.db.commit()
    flash('Succesful')
    return redirect(url_for('justadded'))




@app.route('/')
def index():
    return render_template('index.html')

@app.route('/viewall/')
def viewall():
    return render_template('view_all.html')

if __name__ == '__main__':
    app.run()

表单的html代码是:

    <form action="/new" method="POST">
        <input name="TerminalError" id="searchbar" type="text"         placeholder="Paste Terminal error here...">
        <input name="TerminalSolution" id="searchbar" type="text" placeholder="Paste Terminal solution here...">
        <button type="submit" id="search" class="btn btn-primary">Contribute</button>
        </form>
4

1 回答 1

5

该错误与将数据插入数据库无关,这methods是您/new路由的参数。

而不是这个:

@app.route('/new', methods= "POST")

做这个:

@app.route('/new', methods= ["POST"])

有效方法的列表需要以数组的形式给出。

于 2013-07-30T04:25:56.143 回答