Flask错误:不允许的方法 请求的URL不允许使用该方法

我正在使用Flask开发一个电影推荐系统项目,并添加了一个登录页面。当我点击登录时,显示“Method Not Allowed The method is not allowed for the requested URL。”每当我尝试向我的Flask表单提交数据时,我都会得到以下错误:

Method Not Allowed The method is not allowed for the requested URL.我的代码相关部分如下:当我点击登录时,显示错误方法不被允许。关于这个解决方案的帮助."

#自动补全一些电影

def get_suggestions():        data = pd.read_csv("tmdb.csv")        return list(data["title"].str.capitalize())    app = Flask(__name__)

#主页面

@app.route("/index", methods=["GET", "POST"])def index():    NewMovies = []    with open("movieR.csv", "r") as csvfile:        readCSV = csv.reader(csvfile)        NewMovies.append(random.choice(list(readCSV)))    m_name = NewMovies[0][0]    m_name = m_name.title()    with open("movieR.csv", "a", newline="") as csv_file:        fieldnames = ["Movie"]        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)        writer.writerow({"Movie": m_name})        result_final = get_recommendations(m_name)        names = []        dates = []        ratings = []        overview = []        types = []        mid = []        for i in range(len(result_final)):            names.append(result_final.iloc[i][0])            dates.append(result_final.iloc[i][1])            ratings.append(result_final.iloc[i][2])            overview.append(result_final.iloc[i][3])            types.append(result_final.iloc[i][4])            mid.append(result_final.iloc[i][5])    suggestions = get_suggestions()    return render_template(        "index.html",        suggestions=suggestions,        movie_type=types[5:],        movieid=mid,        movie_overview=overview,        movie_names=names,        movie_date=dates,        movie_ratings=ratings,        search_name=m_name,    )

#登录代码

@app.route("/")@app.route("/login", methods=["GET", "POST"])def login():    if flask.request.method == "POST":        un = request.form["username"]        pwd = request.form["password"]        if un not in database:            return flask.render_template("login.html", info="Invalid User")        else:            if database[un] != pwd:                return flask.render_template("login.html", info="Invalid Password")            else:                return render_template("index.html")    return render_template("login.html")if __name__ == "__main__":    app.run()

回答:

您在@app.route("/")中没有指定methods

新代码:

@app.route("/", methods=["GET", "POST"])@app.route("/login", methods=["GET", "POST"])def login():    if flask.request.method == "POST":        un = request.form["username"]        pwd = request.form["password"]        if un not in database:            return flask.render_template("login.html", info="Invalid User")        else:            if database[un] != pwd:                return flask.render_template("login.html", info="Invalid Password")            else:                return render_template("index.html")    return render_template("login.html")

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注