from flask import Flask, render_template,request
app = Flask(__name__)
#app.route('/',methods=['post'])
def main():
if (request.method=="POST"):
text=request.form('write')
if(text==None):
text=""
else:
text=""
return render_template('form.html',text=text)
if __name__ == "__main__":
app.run(debug=True)
I want to receive only POST method. So I set method option to methods=["post"]. But it always sends HTTP 405 Not Allowed Method error.
<html>
<head>
<title>Using Tag</title>
</head>
<body>
<form method="POST">
<input type="text" name="write">
<input type="submit">
</form>
{{ text }}
</body>
</html>
I want to know reason why this application only sends HTTP 405 response.
To access the HTML form from / path you need to enable both GET and POST request in that route. Otherwise when you try to access the root path / from your browser, you will get the HTTP Method not allowed error.
app.py:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def main():
text = ""
if request.method == "POST":
text = request.form['username']
return render_template('form.html', text=text)
if __name__ == "__main__":
app.run(debug=True)
templates/form.html:
<html>
<head>
<title>Using Tag</title>
</head>
<body>
<form method="POST">
<input type="text" name="write">
<input type="submit">
</form>
{{ text }}
</body>
</html>
Output:
Explanation (Updated):
To access the form value use request.form['INPUT_FIELD_NAME'].
We are making GET and POST requests to the / route. So, we set GET and POST requests in the methods options of the / route. When we are viewing the page using the browser, we make GET request to that page. When we submit the form, we make POST request to that page. In this case, we are storing the form value in the text variable and pass the value to the template. For GET request we are showing the empty text value.
The above snippet is the same as the following snippet:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def main():
if request.method == "POST":
text = request.form['username']
return render_template('form.html', text=text)
elif request.method == "GET":
return render_template('form.html', text="")
if __name__ == "__main__":
app.run(debug=True)
References:
Flask documentation for request object
Related
I'm trying to implement a simple dashboard with Flask that will:
Accept a user text input, with a "submit" button. POST this user input to flask.
Flask accepts this input, does some stuff to it, then makes a GET request to another API.
This GET request returns data and shows it somehow (can just be console.log for now)
As an example, with the star wars API:
User inputs name of a Star Wars character (assume no spelling errors)
Flask reads this input name, and maps it to an ID number, because the Star Wars API accepts id numbers. Form a GET request to the Star Wars API, to get full character information.
For now, we can just console.log character information (e.g. "height", "mass", etc.)
What I have now:
app.py
from flask import Flask, jsonify, request, render_template
import random
import json
app = Flask(__name__)
#app.route("/")
def index():
return render_template('index.html')
#app.route("/form_example", methods=["GET", "POST"])
def form_example():
if request.method == "POST":
language = request.form("character_name")
starwars_dictionary = {"Luke Skywalker":"1", "C-3PO":"2", "R2-D2": "3"}
# starwars_dictionary is a dictionary with character_name:character_number key-value pairs.
# GET URL is of the form https://swapi.co/api/people/<character_number>
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>py-to-JS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<h3>Sample Inputs</h3>
<ul>
<li>Luke Skywalker</li>
<li>C-3PO</li>
<li>R2-D2</li>
</ul>
<form method="POST">
Enter Name: <input type="text" name="character_name"><br>
<input type="submit" value="Submit"><br>
</form>
</body>
</html>
In this current form, when I run the app, it returns "Method not allowed; this method is not allowed for the requested URL".
I'm not sure what I'm missing; it's probably just not wired together properly but I'm not sure what the proper syntax is.
Working version after implementing the accepted answer:
app.py
from flask import Flask, jsonify, request, render_template
import requests
import random
import json
app = Flask(__name__)
#app.route("/index", methods=["GET", "POST"])
def index():
#character_height = "" # init a default value of empty string...seems unwieldy
if request.method == "POST":
character_name = request.form.get("character_name")
# Map user input to a numbers
starwars_dictionary = {"Luke Skywalker":"1", "C-3PO":"2", "R2-D2": "3"}
char_id = starwars_dictionary[character_name]
url = "https://swapi.co/api/people/"+char_id
response = requests.get(url)
response_dict = json.loads(response.text)
character_height = response_dict["height"]
return render_template("index.html", character_height=character_height)
return render_template("index.html")
##app.route("/form_example", methods=["GET", "POST"])
#def form_example():
if __name__ == "__main__":
app.run(debug=True)
index.html
<!DOCTYPE html>
<html>
<head>
<title>py-to-JS</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<h3>Sample Inputs</h3>
<ul>
<li>Luke Skywalker</li>
<li>C-3PO</li>
<li>R2-D2</li>
</ul>
<form method="POST" action="/index">
Enter Name: <input type="text" name="character_name"><br>
<input type="submit" value="Submit"><br>
</form>
{{ character_height }}
</body>
</html>
Probably the form is posting to the / endpoint, because you didn't declare a form action.
Needs to be more like:
<form method="POST" action="/form_example">
Or if you want to get snazzy and use Jinja's url_for function:
<form method="POST" action="{{ url_for('form_example') }}">
EDIT: That said, you could handle this with a single route function:
#app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
language = request.form("character_name")
starwars_dictionary = {"Luke Skywalker":"1", "C-3PO":"2", "R2-D2": "3"}
# Logic to query remote API ges here.
else: # Assume method is GET
return render_template("index.html")
Then make the form action {{ url_for('index') }}
I am closely following the very brief tutorial from Flask-wtf here. I have an issue where after I submit my name in my submit page form, it gives a "405 Method Not Allowed" message instead of directing me to the success page.
from flask import Flask, render_template, redirect
from forms import MyForm
app = Flask(__name__)
app.secret_key = 'mysecretKey'
#app.route('/submit', methods=('GET', 'POST'))
def submit():
form = MyForm()
if form.validate_on_submit():
return redirect('/success')
return render_template('submit.html', form=form)
#app.route('/success')
def success():
return "Well done for entering your name!"
if __name__ == '__main__':
app.run(debug=True)
My form is here:
from flask_wtf import FlaskForm
from wtforms import StringField
from wtforms.validators import DataRequired
class MyForm(FlaskForm):
name = StringField('name', validators=[DataRequired()])
My submit.html code is shown below (just like in the tutorial):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password page</title>
</head>
<body>
<form method="POST" action="/">
{{ form.hidden_tag() }}
{{ form.name.label }} {{ form.name(size=20) }}
<input type="submit" value="Go">
</form>
</body>
</html>
EDIT: The if form.validate_on_submit() condition does not return True so the contents of the loop do not execute. I added a simple print statement in it which didn't execute.
I'd be lying if I pretended to know exactly how all relative paths are resolved. However, you can fix this by changing:
<form method="POST" action="/">
to:
<form method="POST" action="{{ url_for('submit') }}">
It's one of those things that it really does make sense to pass the issue off to the library to figure out. Use url_for which also works in Jinja2 when rendering the template.
The problem is you are not mentioning that the method success should handle POST requests.
from flask import Flask, render_template, redirect
from forms import MyForm
app = Flask(__name__)
app.secret_key = 'mysecretKey'
#app.route('/submit', methods=('GET', 'POST'))
def submit():
form = MyForm()
if form.validate_on_submit():
return redirect('/success')
return render_template('submit.html', form=form)
#app.route('/success')
def success():
return "Well done for entering your name!"
if __name__ == '__main__':
app.run(debug=True)
with
from flask import Flask, render_template, redirect
from forms import MyForm
app = Flask(__name__)
app.secret_key = 'mysecretKey'
#app.route('/submit', methods=['GET', 'POST'])
def submit():
form = MyForm()
if form.validate_on_submit():
return redirect('/success')
return render_template('submit.html', form=form)
# This is the change I made.
#app.route('/success', methods=['POST'])
def success():
return "Well done for entering your name!"
if __name__ == '__main__':
app.run(debug=True)
I'm new to using Flask and I've just been trying to pass a variable between two web pages. The first is a simple form to accept a number with the second page just displaying what is entered.
HTML for the form page:
<!doctype html>
<html>
<body>
<form action ="{{ url_for('return_form', glon="glon") }}" method="post">
Galactic Longitude: <input type="text" name="glon">
<button type="submit">Submit</button>
</form>
</body>
</html>
HTML for the display page:
<!doctype html>
<body>
<p> {{ glon }} </p>
</body>
</html>
The Flask script currently looks like this:
from flask import Flask
from flask import render_template, url_for, request, redirect
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/form/', methods = ['GET', 'POST'])
def form():
if request.method == 'POST':
glon = request.form['glon']
#glat = request.form['glat']
return redirect(url_for('return_form', glon=glon))
return render_template('form.html')
#app.route('/return_form/<glon>', methods = ['GET', 'POST'])
def return_form(glon):
return render_template('return_form.html', glon=glon)
if __name__ == '__main__':
app.run()
At the moment, the second page just displays "glon" instead of the number passed to the form.
I simply want the variable to display on the second page, and eventually use it in the return_form function.
So i didn't got your approach.Below is what i did,I changed the code a bit. Hope this solves your problem.
main.py
from flask import Flask
from flask import render_template, url_for, request, redirect
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/form', methods = ['GET', 'POST'])
def form():
if request.method == 'POST':
glon = request.form['glon']
return render_template('display.html', glon=glon)
# #app.route('/return_form/<glon>', methods = ['GET', 'POST'])
# def return_form(glon):
# return render_template('return_form.html', glon=glon)
if __name__ == '__main__':
app.run()
index.html
<html>
<body>
<form action ="{{ url_for('form') }}" method="post">
Galactic Longitude: <input type="text" name="glon">
<button type="submit">Submit</button>
</form>
</body>
</html>
display.html
<!doctype html>
<body>
<p> {{ glon }} </p>
</body>
</html>
I've implemented a service in Python with Flask, to create the server. My service (MyService) take a query from the user and return a response, like a chatbot. So, i want to return both a text respons modifying the Html template, and a json containing response for using service as command line.
At the moment my service only return a render template,how I can do?
My app:
app = Flask(__name__)
#app.route("/")
def main():
return render_template('index.html')
#app.route("/result", methods=['POST', 'GET'])
def result():
if request.method == 'POST':
query = request.form['query']
response = MyService.retrieve_response(query)
return render_template("index.html", value=response)
if __name__ == "__main__":
app.run()
And my simple index.html:
<!DOCTYPE html>
<html lang="en">
<body>
<h2>Wellcome!</h2>
<form action="http://localhost:5000/result" method="POST">
Make a question:<br>
<br>
<input type="text" name="query" id="query">
<br><br>
<input type="submit" value="submit"/>
</form>
<br>
<h3>Response is: </h3>
<br>
{{value}}
</body>
</html>
You can branch out your return based on request type. If the request is for html text, return the render_template. If the request is for json, return json. For example:
#app.route("/result", methods=['POST', 'GET'])
def result():
if request.method == 'POST':
query = request.form['query']
response = MyService.retrieve_response(query)
if request.headers['Content-Type'] == 'application/json':
return jsonify(...)
return render_template("index.html", value=response)
#dvnguyen's answer is good, but you might consider creating different routes for html and for json. For example:
#app.route("/web/result")
def result_html():
response = MyService.retrieve_response()
return render_template("index.html", value=response)
#app.route("/api/result")
def result_json():
response = MyService.retrieve_response()
return jsonify(response)
The /api or /web prefix makes the intention clear, and also simplifies unit testing.
from flask import *
from twilio import twiml
from twilio.rest import TwilioRestClient
from flask import render_template
import os
#Pull in configuration from system environment variables
TWILIO_ACCOUNT_SID = os.environ.get('Axxxxxx')
TWILIO_AUTH_TOKEN = os.environ.get('Sxxxxxxxxx')
TWILIO_NUMBER = os.environ.get('xxxxxxx')
# create an authenticated client that can make requests to Twilio for your
# account.
#client = TwilioRestClient(account='Axxxxx', token'sxxxxxxxx')
#create a flask web app
app = Flask(__name__)
client = TwilioRestClient(account='Axxxxx', token='Sxxxxx')
#app.route('/')
def homepage():
return render_template('index.html')
#Handling a post request to send text messages.
#app.route('/message', methods=['POST', 'GET'])
def message():
# Send a text message to the number provided
if request.method == 'POST':
message = client.sms.messages.create(to=request.form['Phone_number'],
from_=TWILIO_NUMBER,
body=request.form['body'])
return render_template('message.html')
if __name__ == '__main__':
# Note that in production, you would want to disable debugging
app.run(debug=True)
I am using flask. When i input the number and the text message it gives me this error
Method Not Allowed
The method is not allowed for the requested URL.
You're posting to the wrong endpoint. Your form should look like this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Send an SMS</title>
</head>
<body>
<form action="/message" method="POST">
Cell phone number: <input name="phone_number" type="text" />
Text in here: <input name="body" type="text" />
<button type="submit">Send Text</button>
</form>
</script>
</body>
</html>
(Above, action was changed from / to /message.)
Note: if this is a template run through flask.render_template, you should change
<form action="/message" method="POST">
to
<form action="{{ url_for('message') }}" method="POST">
This is a more sustainable way to use urls in flask, and it will reduce your overhead if you ever need to change the value.