Send Data from a Textbox into Flask

Send data from a textbox into Flask?

Unless you want to do something more complicated, feeding data from a HTML form into Flask is pretty easy.

  • Create a view that accepts a POST request (my_form_post).
  • Access the form elements in the dictionary request.form.

templates/my-form.html:

<form method="POST">
<input name="text">
<input type="submit">
</form>
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def my_form():
return render_template('my-form.html')

@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']
processed_text = text.upper()
return processed_text

This is the Flask documentation about accessing request data.

If you need more complicated forms that need validation then you can take a look at WTForms and how to integrate them with Flask.

Note: unless you have any other restrictions, you don't really need JavaScript at all to send your data (although you can use it).

Send string from textbox together with file into Flask

Seems I've found a solution - move textbox in one form with upload. Dont know is it legal, but seems it's working)

Replace this:

   <h1>Upload new File</h1>

<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form>

<form method=post>
<input name="text">
<input type="submit">
</form>

to this:

  
<h1>Upload new File</h1>

<form method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input name="text">
<input type=submit value=Upload>
</form>

trying to pass value from a textbox into a variable in python with flask

You should only have one route per URL, and perform a check on the request object to determine how to proceed

@app.route('/', methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
# get the form data
text = request.form['input']
# do your prediction here
render_template('index.html', text=text)
return render_template('index.html')

Flask -- Use a button to submit text in a text box (a form)

you have your answer, but to others who will read this question later, and because you have so many mistakes in your pasted code that isn't going to make it any usable for others:

you should place the button inside the form, so your form would look like this:

<style>   textarea {  width: 100%;  height: 200px;  padding: 12px 20px;  box-sizing: border-box;  border: 2px solid #ccc;  border-radius: 4px;  background-color: #f8f8f8;  font-size: 16px;  resize: none;  }</style><form method="POST">   <textarea name="textbox"></textarea>  <button type="submit" name="submit">Submit</button></form>

How to pass value entered in textbox to flask in text format from django?

I have found a solution for the question, but I want to know whether there is any other way to pass data.

views.py

from django.shortcuts import render
import requests

# Create your views here.

def form(request):
return render(request,'hello/index.html')

def output(request):
name1=request.GET["name"]
if not name1:
name1=0
response = requests.get('http://127.0.0.1:5000/'+str(name1)).json()
#name = response.text
return render(request, 'hello/index.html', {'out': response['out']})

html file

<!DOCTYPE html>
<html>
<body>

<form action="output" method="GET">

Enter name: <br/>
<input type="text" name="name"> <br/>

<input type="submit" ><br/>

</form>
<div>
{{out}}
</div>

</body>
</html>

flask file

from flask import Flask,jsonify
app = Flask(__name__)
@app.route('/<name>',methods=['GET'])
def index(name):
return jsonify({'out' : "Hello"+ " "+str(name)})
if __name__ == '__main__':
app.run(debug=True)

How to send text from input fields to MySQL database using a python flask app

"Bad Request The browser (or proxy) sent a request that this server could not understand." it means that through the request object, you are trying to access a request parameter that does not exist.

So the problem is in this piece of code here:

if request.method == "POST":
username = request.form["uname"]
first_name = request.form["fname"]
last_name = request.form["lname"]
password = request.form["pwd"]
email = request.form["email"]
add_new = add_text(username, first_name, last_name, password, email)
print("success")

I recommend that you print the content of request.form print(str(request.form)), which is a dictionary, so you can see what parameters are passed to the endpoint.

Given your html code, the code would become

if request.method == "POST":
username = request.form["Username"]
first_name = request.form["First Name"]
last_name = request.form["Last Name"]
password = request.form["Password"]
email = request.form["Email"]
add_new = add_text(username, first_name, last_name, password, email)
print("success")

How to get HTML Input using Python Flask

Change your form method from GET to POST, as your route only specifies "POST", and will not accept any other requests of a different type:

<form method="POST">

Edit: if you wish to specify both methods, ensure that your route checks for the correct type of request currently being sent when the route is triggered:

@app.route('/', methods=['POST','GET'])
def form_post():
if flask.request.method == 'POST'
userEmail = request.form['userEmail']
userPassword = request.form['userPassword']
return userEmail, userPassword
return flask.render_template('something.html')

Note, however, that you are creating your form on the home route ('/'). It may be best to return a link to the page that has the form code:

@app.route('/')
def home():
return 'Welcome! <a href="/login">login here</a>'

@app.route('/login', methods=['GET', 'POST']):
if flask.request.method == 'POST'
userEmail = request.form['userEmail']
userPassword = request.form['userPassword']
return flask.redirect('/')
return flask.render_template('form_filename.html')


Related Topics



Leave a reply



Submit