Search code examples
pythonvisual-studioauthenticationflaskartificial-intelligence

Flask error : Method Not Allowed The method is not allowed for the requested URL


I am making project on movie recommendation system using flask and i added login page when i click on login it shows Method Not Allowed The method is not allowed for the requested URL. "m getting the following error whenever I try to submit data to my Flask form:

Method Not Allowed The method is not allowed for the requested URL.
Relevant parts of my code are as follows:
when i click on login it shows error method Not allowed.
help regarding this solution.
"

#auto complete some movies

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

app = Flask(__name__)

#main page

@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,
    )

#login code

@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()

Solution

  • You didn't specify methods in @app.route("/").

    New Code:

    @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")