I have a html page where i have some forms where i write the crust, size and flavor of a pizza. I try to get those data via request.form, but i get the error
'werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.'
I searched for it and most people would say it is because the form would be missing, or not maching the name. But i guess it is.
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///pizza.db'
db = SQLAlchemy(app)
class Order(db.Model):
order_id = db.Column(db.Integer, primary_key=True)
crust = db.Column(db.String(80), unique=True, nullable=False)
flavor = db.Column(db.String(120), unique=True, nullable=False)
size = db.Column(db.String(120), unique=True, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
def __repr__(self):
return '<Pedido %r>' % self.order_id
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'GET':
return render_template('index.html')
elif request.method == 'POST':
flavor = request.form['flavor']
size = request.form['size']
crust = request.form['crust']
new_order = Order(crust=crust, flavor=flavor, size=size)
try:
db.session.add(new_order)
db.session.commit()
return redirect('/')
except:
return 'error posting data...'
if __name__=="__main__":
app.run(debug=True)
{% extends 'base.html' %}
{% block head %}
<title>Task Master</title>
{% endblock %}
{% block body %}
<div class="content">
<h1 >Pizza Master</h1>
<table class="center">
<tr>
<th>Sabor</th>
<th>Tamanho</th>
<th>Borda</th>
</tr>
<td>
<form action="/" method="POST">
<input type="text" name="flavor" id="flavor" required>
</form>
</td>
<td>
<form action="/" method="POST">
<input type="text" name="size" id="size" required>
</form>
</td>
<td>
<form action="/" method="POST">
<input type="text" name="crust" id="crust" required>
</form>
</td>
</table>
<br>
<form action="/" method="POST">
<input type="submit" value="Pedir pizza">
</form>
</div>
{% endblock %}