Reputation: 127
I've been using python flask as well as html, in order to create a small website, (just as a hobby while I'm off school), I created a form in html, and saved it within the templates folder of the project. I also then added a function within the python script, so when a button is clicked of the webpage it would redirect the user back to the home page (index.html), however when I have tested the webpage and clicked on the button on the webpage (with the flask server running) a "400 bad request" page is shown
Python Code:
#Python code behind the website
import datetime
from flask import Flask, session, redirect, url_for, escape, request, render_template
print ("started")
def Log(prefix, LogMessage):
timeOfLog = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S" + " : ")
logFile = open("Log.log", 'a')
logFile.write("[" + timeOfLog + "][" + prefix + "][" + LogMessage + "] \n")
logFile.close()
app = Flask(__name__)
@app.route('/')
def my_form():
print ("Acessed index")
return render_template("index.html")
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']#requests text from the text form
processed_text = text #does nothing
user = "" #use poss in future to determin user
logFile = open("MessageLog.msglog", 'a')#opens the message log file in append mode
logFile.write(text + "\n")#stores the inputted message in the message log file
logFile.close()
#print (text)
Log("User Message", (user + text))#uses the main log file to store messages aswell as errors
print ("Accessing index")
return render_template("test.html")
@app.route('/test', methods=['POST'])
def test():
#text = request.form['text']
print ("Test page function")
#return "hello"
return render_template("index.html")
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')
HTML code: -->
<body>
<h1>Test Page</h1>
<form method="POST">
<input type="submit" name="my-form" value="Send">
</form>
</body>
Stack Track:
Upvotes: 0
Views: 296
Reputation: 11039
You need to post your form to the correct URL:
<body>
<h1>Test Page</h1>
<form method="POST" action='/test'>
<input type="submit" name="my-form" value="Send">
</form>
</body>
By default if you don't add an action
attribute to an HTML form it will just perform it's defined method to the URL you are currently on. You can add an action
attribute to change that behavior.
You can also do this with the url_for()
function. This is a bit safer as URL's tend to change more often than your view method names:
<body>
<h1>Test Page</h1>
<form method="POST" action="{{ url_for('test') }}">
<input type="submit" name="my-form" value="Send">
</form>
</body>
You pass the name of the view method (not it's URL) as a string to the function. Be careful to use the right quotes.
Note that it's slightly confusing to have 2 views for the same URL. Usually something like this is done although YMMV but consider it for your application:
@app.route('/someurl')
def some_view():
if request.method == "POST":
# handle POST
else:
# handle GET
Upvotes: 1