Answer the question
In order to leave comments, you need to log in
How to return value from form?
Hello everyone, I really need help!
I have a Flask + SQLAlchemy database code. There is also HTML. I did all the operations to enter values into the database through the form. Below are the codes:
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///mydb.db'
db = SQLAlchemy(app)
class Subject(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
names = db.Column(db.String(100))
place = db.Column(db.Integer)
total = db.Column(db.Integer)
power = db.Column(db.String(50))
@app.route('/', methods=['GET', 'POST'])
def concrete():
if request.method == 'GET':
return render_template('index.html')
# /subj если подключать то можно дополнять БД.
@app.route('/subj', methods=['GET', 'POST'])
def get():
if request.method == 'GET':
subjects = Subject.query.all()
user = Subject(names='', place='', total='', power='')
return render_template('subj.html', subjects=subjects, user=user)
else:
names = request.form['names']
place = request.form['place']
total = request.form['total']
power = request.form['power']
newSubject = Subject(names=names, place=place,
total=total, power=power)
db.session.add(newSubject)
db.session.commit()
return redirect('/subj')
if __name__ == '__main__':
app.run(debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="look.css">
<title>Калькулятор баллов</title>
</head>
<body>
<div class="header">
<h2>Калькулятор баллов</h2>
</div>
<div class="mainer">
<form action="/" method="POST" target="_blank"></form>
<p><b>Выберите форму обучения:</b></p>
<p><input name="forma" type="radio" value={{direc}}> Очная</p>
<p><input name="forma" type="radio" value={{derec}}> Заочная</p>
<input type="submit" value="Выбрать" />
</form>
</div>
<div class="list_text">
<p><b>Какие предметы Вы сдавали по ЕГЭ:</b></p>
</div>
<div class="menu_test">
<form method="POST" action="/" name="menu_test">
<label for="maths">Математика: </label>
<input id="math" type="text" name="math" placeholder="100" size="2" required value={{math}}>
<p><label for="rus">Русский язык: </label>
<input id="rus" type="text" name="rus" placeholder="100" size="2" required value={{rus}}></p>
<p><label for="bio">Биология: </label>
<input id="bio" type="text" name="bio" placeholder="100" size="2" value={{bio}}></p>
<p><label for="geo">География: </label>
<input id="geo" type="text" name="geo" placeholder="100" size="2" value={{geo}}></p>
<label for="phis">Физика: </label>
<input id="phis" type="text" name="phis" placeholder="100" size="2" value={{phis}}>
<button class="btn_block" type="submit">Показать доступные направления</button>
</form>
</div>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Your code on the flask has nothing to do with forms in html,
look, maybe you mixed up the files.
If you use the request.form method,
then the key in the method and in the form must match,
i.e. for
in the html form there should be such an input:
I hope it is clear that the key here is username
And your keys in the code and the form do not match.
In addition, it is customary to use WTF-Forms to process and validate html forms in Flask.
The link about him is very detailed and intelligible: https://habr.com/en/post/346342/
name = request.form["username"]
<input type="text" name = "username" blablabla >
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question