-1

I'm working on a flask project and getting a method not found error. I am simply trying to go from my homepage to my game page at the press of submit button. I can't figure out where I am going wrong.

I know this question has been asked several times already but I have tried everything in those questions and am still getting an error.

Here is my routes file:

from flask import render_template
from app import app
from app.forms import LoginForm

@app.route('/')
@app.route('/homepage', methods=['GET', 'POST'])
def homepage():
    form = LoginForm()
    if form.validate_on_submit():
        return redirect(url_for('index'))
    return render_template('homepage.html', title='Welcome', form=form)

@app.route('/index')
def index():

    return render_template('index.html')

Here is my html code on the homepage containing the button:

<html>
    <head>
        {% if title %}
            <title>{{ title }} - CatanAI</title>
        {% else %}
            <title>CatanAI</title>
        {% endif %}
    </head>
        <h1>Welcome to Settlers of Catan AI</h1>
    <body>

        <form method="post" action='{{url_for('index')}}'>

        <p>{{ form.submit() }}</p>
        </form>
    </body>
</html>

And here is the html for the code I am trying to route to:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Catan</title>
        <script src="{{ url_for('static', filename="css/main.css")}}"></script>
    </head>
    <body>

        <canvas id="canvas" ></canvas>
        <script src="{{ url_for('static', filename="js/board.js")}}"></script>
        <script src="{{ url_for('static', filename="js/game.js")}}"></script>
        <script src="{{ url_for('static', filename="js/location.js")}}"></script>
        <script src="{{ url_for('static', filename="js/main.js")}}"></script>
        <script src="{{ url_for('static', filename="js/player.js")}}"></script>
    </body>
</html>

When I press the button I am getting the 405 method not allowed. I appreciate any help.

Orgenplop
  • 17
  • 7

1 Answers1

0

The index view did not allow post method.

You need to replace

<form method="post" action='{{url_for('index')}}'>

by

<form method="post" action='{{url_for('homepage')}}'>

If you want to execute post method in the index url, you need to add methods=['GET', 'POST'] to your index view like below:

@app.route('/index', methods=['GET', 'POST'])
def index():
    # write your code here
    return render_template('index.html')
ASSILI Taher
  • 1,054
  • 2
  • 7
  • 11