问题描述
我完成了教程要点上的所有说明(只是复制并粘贴),但是当我尝试添加学生条目时,即它会显示添加学生"
I did all that was stated on tutorial point (just copied and pasted), but when I tried to add a student entry,i.e. ‘Add Student’ it gives
错误请求浏览器(或代理)发送了该服务器无法理解的请求.
Bad RequestThe browser (or proxy) sent a request that this server could not understand.
请告知本教程是否有问题.
Please advise if there is anything wrong with the tutorial.
在app.py的def new()中的这一行失败了:
It failed at this line within def new(), in app.py:
student = students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin'])
任何人对此进行标榜.请注意,本教程中充满了错别字和错误的缩进.我只是一个学生.将此关闭,我将一无所获.
Whoever is flagging this down. Note that it is the tutorial that is filled with typos and wrong indentations. I am only a student. Shut this down and I will learn nothing.
参考: http://www.tutorialspoint.com/flask/flask_sqlalchemy.htm
from flask import Flask, request, flash, url_for, redirect, render_template
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.sqlite3'
app.config['SECRET_KEY'] = "random string"
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('/')
def show_all():
return render_template('show_all.html', Students = Students.query.all() )
@app.route('/new', methods = ['GET', 'POST'])
def new():
if request.method == 'POST':
if not request.form['name'] or not request.form['city'] or not request.form['addr']:
flash('Please enter all the fields', 'error')
else:
print "1";
student = Students(request.form['name'], request.form['city'], request.form['addr'], request.form['pin'])
print "1";
db.session.add(student)
print "1";
db.session.commit()
print "1";
flash('Record was successfully added')
print "=======>>>>>>>>>";
return redirect(url_for('show_all'))
return render_template('new.html')
if __name__ == '__main__':
db.create_all()
app.run(debug = True)
推荐答案
该教程在学生班级中存在缩进问题.构造函数代码应缩进一级,以使其成为学生类的一种方法.
The tutorial has an indentation problem in the student class.The constructor code should be indented one level so it becomes a method of the student class.
正确的代码:(请注意以下代码中的"def init (自身,名称,城市,地址,地址,PIN):"的缩进)
Corrected code: (note the indent of "def init(self, name, city, addr,pin):" in the code below)
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
原因是,如果该缩进不存在,python将不会将此函数视为学生类的构造函数.因此找不到具有匹配数量的参数的构造函数,从而导致错误.
The reason is, if that indent is not there, python will not see this function as a constructor of the student class. So the constructor with the matching number of arguments is not found, resulting in the error.
这篇关于TutorialsPoint-Flask – SQLAlchemy无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!