The "send..." button is not working, index page is running, drop-down menu ok, but when I click the "send..." nothing happen. (Result.html is ok, but empty of course) Any idea, what is the problem?
I've got these .py file:
from flask import Flask, request, render_template
from flask_wtf import Form
from flask_wtf import FlaskForm
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def index():
return render_template('index.html')
#app.route('/result/', methods=['POST', 'GET'])
def result():
vehicle = request.form.get('wine')
year = request.form.get('year')
return render_template('result.html', vehicle=vehicle, year=year)
And two .html of course. index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Submitting</h1>
<form action="/result" method="POST">
<label for="wine">wine</label>
<select id="wine" name="wine">
<option>red</option>
<option>white</option>
<option>rose</option>
</select>
<label for="year">Year</label>
<select id="year"name="year">
<option>1972</option>
<option>1999</option>
<option>2010</option>
</select>
</form>
<br>
<button type="submit" value="submit">send...</button>
</body>
</html>
The result.html:
<!DOCTYPE html>
<html>
<body>
{{ vehicle }} {{ year }}
</body>
</html>
replace your HTML with this HTML. you need to put submit button inside form tag to make it work.
<body>
<h1>Submitting</h1>
<form action="/result" method="POST">
<label for="wine">wine</label>
<select id="wine" name="wine">
<option>red</option>
<option>white</option>
<option>rose</option>
</select>
<label for="year">Year</label>
<select id="year"name="year">
<option>1972</option>
<option>1999</option>
<option>2010</option>
</select>
<button type="submit" value="submit">send...</button>
</form>
<br>
</body>
</html>
maybe you need to put your button in form tag ?
<form action="/result" method="POST">
...
<button type="submit" value="submit">send...</button>
</form>
Related
I'm following an online course to learn about Flask.
I built a very simple set of HTML/Java/Flask.
On a POST or GET, I've tried both it's not passing the default value, world to the greet.html page when the input text is not populated with any text, just empty.
The specific line of code is:
return render_template("greet.html", name=request.form.get("name", "world"))
Through trial and error I've found the trigger in the following line.
<input id="name" autocomplete="off" autofocus placeholder="Enter Name" type="text" name="name">
If I remove the name="name" option from the input, the world is passed when nothing has been entered.
Here's the full test code:
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
return render_template("greet.html", name=request.form.get("name", "world"))
return render_template("index.html")
app.run(host='0.0.0.0', port=80)
layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
index.html
{% extends "layout.html" %}
{% block body %}
<form action="/" method="post">
<input id="name" autocomplete="off" autofocus placeholder="Enter Name" type="text" name="name">
<input type="submit">
</form>
{% endblock %}
greet.html
{% extends "layout.html" %}
{% block body %}
hello, {{ name }}
{% endblock %}
I expected this to just work.
As stated I already found the trigger, just not sure why.
Would like to see if anyone else has ran into this or if it's something I'm doing wrong or overlooked.
Using:
Flask 2.2.2
Python 3.9.0
Task in general:
The user should type in a text and the server should receive this text.
Problem
I get this error message when i run the code in VScode below.
What i am doing wrong?
My code files:
home.html
<!DOCTYPE html>
<html>
<body>
<h1>home</h1>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<body>
<h1>login</h1>
<form action="http://127.0.0.1:5500/login" method="POST">
<input type="text" placeholder="write your text here" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
local.py
from flask import Flask, request, render_template
app = Flask(__name__, template_folder='template')
#app.route('/')
def index():
#return 'tesddt'
return render_template('home.html')
#app.route('/login', methods=['GET', 'POST'])
def login():
print(request.form['name']) # should display 'website'
return render_template('login.html') #'Received !' + website # response to your request.
if __name__ == '__main__':
app.run(host="127.0.0.1", port=5500)
I use this Url: http://127.0.0.1:5500/login in my browser.
<!DOCTYPE html>
<html>
<body>
<h1>login</h1>
<form action="/login" method="POST">
<input type="text" placeholder="write your text here" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Use the above code and check
Complete newbie to web dev so I suspect this might be more of an architectural problem rather than a technical one..
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
#app.route('/', methods=("POST", "GET"))
def index():
if request.method == 'GET':
return render_template("templ.html")
if request.method == 'POST':
if 'submit' in request.form:
return str([request.form[val] for val in ['input1', 'input2']])
if 'cancel' in request.form:
return redirect(url_for('index'))
app.run(host='0.0.0.0', port='5000')
html:
<!doctype html>
<form action="." method="POST">
<div>
<input type="text" name="input1" required="required">
<input type="text" name="input2" required="required">
</div>
<div>
<button name="submit">Submit</button>
<button name="cancel">Cancel</button>
</div>
</form>
Question: Can I skip 'required' when cancel button is pressed with this simple design or do I need to employ flask_wtf with wtforms.validators? Could I get a working minimal script with my example code please?
There may be a more efficient way of handling it, but this will work as it places the 'Cancel' button in a separate form.
<form action="." method="POST" id ="CancelForm"></form>
<form action="." method="POST">
<div>
<input type="text" name="input1" required="required">
<input type="text" name="input2" required="required">
</div>
<div>
<button name="submit">Submit</button>
<button type="submit" form="CancelForm">Cancel</submit>
</div>
</form>
You may want to give it a different action so you can handle 'cancel' differently
Apologies for my ignorance on the subject. I am just getting my feet wet with Web Dev with Python and Flask.
I am trying to create an app that will take a string from an input field and convert it into a hash and display it on an output page. However, I am not sure if my form is set up correctly.
When I run the application it only returns a false value and displays that the user has not inputted anything even when I type in a random string.
app.py
from flask import Flask, render_template, request, url_for, flash, redirect
from message import MessageForm, validators, ValidationError
from cryptography.fernet import Fernet
app = Flask(__name__)
app.secret_key = 'development'
key = Fernet.generate_key()
f = Fernet(key)
#app.route('/', methods=['GET', 'POST'])
def home():
form = MessageForm(request.form)
if request.method == 'POST' and form.validate_on_submit():
user_message = form.message_field.data
e = user_message.encode(encoding='UTF-8')
token = f.encrypt(e)
output = bytes.decode(token)
return redirect('output.html', output=output)
return render_template('index.html', form=form)
if __name__ == ('__main__'):
app.run(debug=True)
message.py
from wtforms import StringField, SubmitField, validators
from flask_wtf import FlaskForm
from wtforms.validators import DataRequired, ValidationError
class MessageForm(FlaskForm):
message_field = StringField('Please enter the message you would like to
encrypt:', [validators.Required('Please enter a message!')])
submit = SubmitField('Submit')
HTML Form
{% extends 'layout.html' %}
{% block body %}
{{ form.csrf_token }}
<br />
<form action="/" method="POST">
<div class="form-group">
<label style="font-weight: bold;">{{ form.message_field.label }}</label>
<input type="text" class="form-control" name="message" id="message">
<br />
<button type="submit" class="btn btn-primary btn-lg btn-block">Encrypt Message</button>
</div>
</form>
{% for message in form.message_field.errors %}
<div class="alert alert-danger" role="alert">
{{ message }}
</div>
{% endfor %}
{% endblock %}
What I would like is to have the app return back an error if nothing is entered but run the application correctly if something is entered.
I hope that makes sense and as previously stated please excuse my ignorance.
Your help is very much appreciated.
Maybe you could try:
user_message = request.form.get("message")
instead of
user_message = form.message_field.data
In the HTML Form, try to put the csrf_token after the form declaration like this:
<br />
<form action="/" method="POST">
{{ form.csrf_token }}
<div class="form-group">
<label style="font-weight: bold;">{{ form.message_field.label }}</label>
<input type="text" class="form-control" name="message" id="message">
<br />
<button type="submit" class="btn btn-primary btn-lg btn-block">Encrypt Message</button>
</div>
</form>
I am getting this error:
"The method is not allowed for the requested URL" when i try to submit a request.
Here's my python code:
from flask import Flask,render_template,request
import requests
api_address='http://api.openweathermap.org/data/2.5/weather?appid=014093f04f1d04c9e512539a36d4aaa9&q='
app=Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
#app.route('/weather',methods=['POST'])
def weather():
city=request.form['city_name']
url=api_address + city
json_data=requests.get(url).json()
temp_k=float(json_data['main']['temp'])
temp_c=temp_k-273.15
return render_template("weather.html",temp=temp_c)
if __name__=='__main__':
app.run(debug=True)
Here's the HTML code for 'home.html':
<!DOCTYPE html>
<html>
<body>
<p>
<form action='http://127.0.0.1:5000/' method="post">
City Name: <input type="text" name="city_name"> <br/>
<input type="submit" name="form" value="Submit">
</form>
</p>
</body>
</html>
HTML code for 'weather.html' is:
<!DOCTYPE html>
<html>
<body>
<h1>Temperature: {{temp}} degree celcius</h1>
<h1>Condition: {{desc}}</h1>
</body>
</html>
What should i do???
You are posting to http://127.0.0.1:5000/ instead of /weather
Try this:
<!DOCTYPE html>
<html>
<body>
<p>
<form action='/weather' method="post">
City Name: <input type="text" name="city_name"> <br/>
<input type="submit" name="form" value="Submit">
</form>
</p>
</body>
</html>