Gary Cooper Gary Cooper - 2 months ago 6x
Python Question

Employing Post Method in Flask

Using the following:

from flask import Flask, render_template
import beautiful_soup_tidal

app = Flask(__name__)

def form():
return render_template('form_submit.html')

@app.route('/richmond', methods=['POST'])
def richmond():
someTides = beautiful_soup_tidal.getTides()
return render_template('richmond.html',someTides=someTides)

if __name__ == "__main__":

And attempting to render the following (richmond.html):

<div id="content" class="form-group">
<form method="post" action="/richmond">

<label style="vertical-align: middle;">channel depth at mean low water
<input type="number" step="0.1" value = "34.5" name="channelDepth"/>FEET</label><br><br>

<label style="vertical-align: middle;">required underkeel clearance
<input type="number" step="0.1" value = "2" name="underkeelClearance"/>FEET</label><br><br>

<button type="submit" class="btn btn-primary">Submit</button>


I get the following error: 'The method is not allowed for the requested URL.'

If I delete ', methods=['POST']' in the first section the template renders.

The question: How do I render the template successfully using the post method?


i believe this line should also include GET so that you can render the html form first time round before you actually click submit to post it.

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

so it would change to

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