我正在使用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")