Method Not Allowed Flask Error 405

405: Method Not Allowed. Why am I getting this error in Flask

Update:

When you redirect you will eventually send a GET request to your endpoint, however your endpoint only supports POST.
Therefore you should make a different endpoint then, which supports GET and hosts the template you referenced above. Then the form of the template can send a POST to the add_ev endpoint, and then the add_ev redirects to the new GET endpoint and the loop is closed.

Previous suggestion:

Could you try adding GET as a supported method to the below endpoint?

@app.route("/add_cars", methods=['GET', 'POST'])
def add_ev():
# add ev's
...

405 Method Not Allowed When Redirecting in Flask within POST route

Firstly, Thank you @Henry for the inspiration to finding a solution.

Secondly, I have learnt that including the entire function in the original question, and not just what I feel may be relevant, may have resolved this much sooner.

The Answer.

@Henry mentioned that as per the docs for url_for() - "Variable arguments that are unknown to the target endpoint are appended to the generated URL as query arguments."

This is my take on the situation.

The submit_review() function was returning before the form data was being used, meaning that the form data was unknown to the target endpoint.

Function Before Fix:

@app.route('/submit_review/<game_id>', methods=['POST'])
def submit_review(game_id):
"""
Adds users review to database.
"""

existing_game = Game.query.filter_by(igdb_id=game_id).first()

if not existing_game:
igdb_game_data = get_game_data_by_id(game_id)[0]
igdb_game_artwork = get_game_artwork(game_id)
igdb_game_cover = get_game_cover_art(game_id)

game = Game(
name=igdb_game_data['name'],
artwork=json.dumps(igdb_game_artwork),
summary=igdb_game_data['summary'],
igdb_id=igdb_game_data['id'],
cover_art=igdb_game_cover
)

db.session.add(game)
db.session.commit()

user = User.query.filter_by(username=session['username']).first()
game = Game.query.filter_by(igdb_id=game_id).first()

existing_review = Review.query.filter_by(user_id=user.id,
game_id=game.id).first()

if existing_review:
print(request)
flash('You have already created a review for this game')
return redirect(url_for('manage'))

review = Review(
user_id=user.id,
game_id=game.id,
rating=float(request.form.get('review-rating')),
heading=request.form.get('review-heading'),
liked_text=request.form.get('liked-text'),
disliked_text=request.form.get('disliked-text'),
hours=int(request.form.get('review-hours')),
)

db.session.add(review)
db.session.commit()

flash('Review added successfully')
return redirect(url_for('home'))

By moving where the form data is used, I got the expected results, functionality is correct, as the review is not added to the database if a review for that game, by the same user is present.

Function After Fix:

@app.route('/submit_review/<game_id>', methods=['POST'])
def submit_review(game_id):
"""
Adds users review to database.
"""

existing_game = Game.query.filter_by(igdb_id=game_id).first()

if not existing_game:
igdb_game_data = get_game_data_by_id(game_id)[0]
igdb_game_artwork = get_game_artwork(game_id)
igdb_game_cover = get_game_cover_art(game_id)

game = Game(
name=igdb_game_data['name'],
artwork=json.dumps(igdb_game_artwork),
summary=igdb_game_data['summary'],
igdb_id=igdb_game_data['id'],
cover_art=igdb_game_cover
)

db.session.add(game)
db.session.commit()

user = User.query.filter_by(username=session['username']).first()
game = Game.query.filter_by(igdb_id=game_id).first()

existing_review = Review.query.filter_by(user_id=user.id,
game_id=game.id).first()

review = Review(
user_id=user.id,
game_id=game.id,
rating=float(request.form.get('review-rating')),
heading=request.form.get('review-heading'),
liked_text=request.form.get('liked-text'),
disliked_text=request.form.get('disliked-text'),
hours=int(request.form.get('review-hours')),
)

if existing_review:
print(request)
flash('You have already created a review for this game')
return redirect(url_for('manage'))

db.session.add(review)
db.session.commit()

flash('Review added successfully')
return redirect(url_for('home'))

Flask 405 Error Method Not Allowed on Post Request

Try changing the first route to

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

How to solve 405 Method Not Allowed (flask)

Just find out the issue, I created 2 app instances, I move the instantiation into another file then import it, everything works!

Flask - POST Error 405 Method Not Allowed

Your form is submitting to / when the method is routed for /template unless that is a typo, you should adjust your form's action attribute to point at the template view: action="{{ url_for('template') }}"

Flask 405 Method Not Allowed

Flask is not finding the POST method handler for the /register/ endpoint. You're mixing add_url_rule and @route. You just need the former if you want pluggable views.

I would recommend using the MethodView approach here. Have a separate class for each template and define the get() and post() methods within that.

app.py:

from flask import Flask
from views import Register, Login, Index

app = Flask(__name__)

app.add_url_rule('/', view_func=Index.as_view("index"))
app.add_url_rule('/login/', view_func=Login.as_view("login"))
app.add_url_rule('/register/', view_func=Register.as_view("register"))

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

views.py:

from flask import Flask,render_template,request,redirect,abort
from flask.views import MethodView

class Register(MethodView):
def get(self):
return render_template('register.html')

def post(self):
req = request.form
email = req.get("email")
password = req["password"]
phonenumber = request.form["phonenumber"]
if email == "" or password == "" or phonenumber == "":
feedback = "Please fill the form"
alert = "fail"
else:
feedback = "Account created!"
alert = "good"
return render_template('register.html', feedback=feedback, alert=alert)

class Index(MethodView):
def get(self):
print("CIAO")
return render_template('index.html')

class Login(MethodView):
def get(self):
return render_template('login.html')


Related Topics



Leave a reply



Submit