如何解决我试图将表单数据放入show.html页面但它不起作用我正在使用flask_sqlalchemy我在烧瓶中乞讨
我希望能够将数据发送到我的Flask应用。我尝试访问request.form,但这是一个空字符串。您如何访问请求数据? 我希望能够将数据发送到我的Flask应用程序。我尝试访问request.form,但这是一个空字符串。您如何访问请求数据?
我的app.py文件
from flask import Flask,render_template,request,redirect,url_for
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///students.sqlite3'
db=SQLAlchemy(app)
class students(db.Model):
id=db.Column('student_id',db.Integer,primary_key=True)
name=db.Column(db.String(100))
city=db.Column(db.String(50))
addr=db.Column(db.String(200))
pin=db.Column(db.String(10))
def __init__(self,name,city,addr,pin):
self.name=name
self.city=city
self.addr=addr
self.pin=pin
@app.route('/',methods=['GET','POST'])
def show_all():
return render_template('show.html',students=students.query.all())
@app.route('/new','POST'])
def new():
if request.method == 'POST':
student=students(request.form['name'],request.form['city'],request.form['addr'],request.form['pin'])
db.session.add(student)
db.session.commit()
return redirect(url_for('show_all'))
return render_template('new.html')
if __name__ == "__main__":
app.run(debug=True)
我的new.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/" method="post">
<input type="text" name="name" placeholder="Your name"><br>
<input type="text" name="city" placeholder="Your city"><br>
<input type="text" name="addr" placeholder="your address"><br>
<input type="text" name="pin" placeholder="your pin"><br>
<input type="submit" valu='Submit'>
</form>
</body>
</html>
我的show.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>students <a href="{{url_for('new')}}">Add student</a></h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>City</th>
<th>Addr</th>
<th>Pin</th>
</tr>
</thead>
<tbody>
{% for student in students %}
<tr>
<td>{{ student.name }}</td>
<td>{{ student.city }}</td>
<td>{{ student.addr }}</td>
<td>{{ student.pin }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
解决方法
在new.html
中,您正在将数据发送到根路径/
而不是/new
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。