Python 烧瓶表单类型错误:类型为'的对象;int';没有len()
我想用flask做一份登记表。我的代码看起来很干净,显示了所有正确的数据类型。该错误还强调了myPython 烧瓶表单类型错误:类型为'的对象;int';没有len(),python,flask,flask-sqlalchemy,flask-wtforms,Python,Flask,Flask Sqlalchemy,Flask Wtforms,我想用flask做一份登记表。我的代码看起来很干净,显示了所有正确的数据类型。该错误还强调了myroutes.py中的if form.validate\u on_submit():。我现在迷路了,我真的不知道该怎么办 编辑:我需要它来成功发布数据。请帮帮我 路线: from flask import render_template, url_for, flash, redirect from registration import main from registration.forms imp
routes.py
中的if form.validate\u on_submit():
。我现在迷路了,我真的不知道该怎么办
编辑:我需要它来成功发布数据。请帮帮我
路线:
from flask import render_template, url_for, flash, redirect
from registration import main
from registration.forms import RegistrationForm
from registration.models import Student, Colleges
@main.route("/")
def index():
return redirect(url_for('register'))
@main.route("/register", methods=['GET','POST'])
def register():
form = RegistrationForm()
form.college.choices = [(c.id, c.college) for c in Colleges.query.all()]
if form.validate_on_submit():
college = Colleges.query.filter_by(college=form.college.data).first()
reg_student = Student(student_number=form.student_number.data,last_name=form.last_name.data,first_name=form.first_name.data,college=college.id,section=form.section.data,email=form.email.data)
db.session.add(reg_student)
db.session.commit()
return render_template('register.html', form=form)
@main.route("/admin")
def admin():
return "Students list"
{% extends "base.html" %}
{% block content %}
{{ form.hidden_tag() }}
<form method="POST">
<fieldset>
<legend>Register:</legend>
{{ form.student_number.label }}
{{ form.student_number(class="form-control form-control-lg") }}
{{ form.last_name.label }}
{{ form.last_name(class="form-control form-control-lg") }}
{{ form.first_name.label }}
{{ form.first_name(class="form-control form-control-lg") }}
{{ form.college.label }}
{{ form.college(class="form-control form-control-lg") }}
{{ form.section.label }}
{{ form.section(class="form-control form-control-lg") }}
{{ form.email.label }}
{{ form.email(class="form-control form-control-lg") }}
{{ form.submit(class="btn btn-outline-info") }}
</fieldset>
</form>
{% endblock %}
型号:
from registration import db
class Colleges(db.Model):
id = db.Column(db.Integer, primary_key=True)
college = db.Column(db.String(120), nullable=False)
students = db.relationship('Student', backref='department', lazy=True)
def __repr__(self):
return f"College('{self.college}')"
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_number = db.Column(db.String(11), unique=True,nullable=False)
last_name = db.Column(db.String(60), nullable=False)
first_name = db.Column(db.String(60), nullable=False)
college_id = db.Column(db.Integer, db.ForeignKey('colleges.id'), nullable=False)
section = db.Column(db.String(10), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f"Student('{self.student_number}','{self.last_name}','{self.first_name}','{self.college_id}','{self.section}','{self.email}')"
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField, IntegerField, SelectField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class RegistrationForm(FlaskForm):
student_number = IntegerField('Student Number', validators=[DataRequired(),Length(min=11,max=11)])
last_name = StringField('Last Name',validators=[DataRequired()])
first_name = StringField('First Name',validators=[DataRequired()])
college = SelectField('College', coerce=int,validators=[DataRequired()])
section = StringField('Section', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Sign Up')
表格:
from registration import db
class Colleges(db.Model):
id = db.Column(db.Integer, primary_key=True)
college = db.Column(db.String(120), nullable=False)
students = db.relationship('Student', backref='department', lazy=True)
def __repr__(self):
return f"College('{self.college}')"
class Student(db.Model):
id = db.Column(db.Integer, primary_key=True)
student_number = db.Column(db.String(11), unique=True,nullable=False)
last_name = db.Column(db.String(60), nullable=False)
first_name = db.Column(db.String(60), nullable=False)
college_id = db.Column(db.Integer, db.ForeignKey('colleges.id'), nullable=False)
section = db.Column(db.String(10), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f"Student('{self.student_number}','{self.last_name}','{self.first_name}','{self.college_id}','{self.section}','{self.email}')"
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField, IntegerField, SelectField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class RegistrationForm(FlaskForm):
student_number = IntegerField('Student Number', validators=[DataRequired(),Length(min=11,max=11)])
last_name = StringField('Last Name',validators=[DataRequired()])
first_name = StringField('First Name',validators=[DataRequired()])
college = SelectField('College', coerce=int,validators=[DataRequired()])
section = StringField('Section', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired(), Email()])
submit = SubmitField('Sign Up')
register.html:
from flask import render_template, url_for, flash, redirect
from registration import main
from registration.forms import RegistrationForm
from registration.models import Student, Colleges
@main.route("/")
def index():
return redirect(url_for('register'))
@main.route("/register", methods=['GET','POST'])
def register():
form = RegistrationForm()
form.college.choices = [(c.id, c.college) for c in Colleges.query.all()]
if form.validate_on_submit():
college = Colleges.query.filter_by(college=form.college.data).first()
reg_student = Student(student_number=form.student_number.data,last_name=form.last_name.data,first_name=form.first_name.data,college=college.id,section=form.section.data,email=form.email.data)
db.session.add(reg_student)
db.session.commit()
return render_template('register.html', form=form)
@main.route("/admin")
def admin():
return "Students list"
{% extends "base.html" %}
{% block content %}
{{ form.hidden_tag() }}
<form method="POST">
<fieldset>
<legend>Register:</legend>
{{ form.student_number.label }}
{{ form.student_number(class="form-control form-control-lg") }}
{{ form.last_name.label }}
{{ form.last_name(class="form-control form-control-lg") }}
{{ form.first_name.label }}
{{ form.first_name(class="form-control form-control-lg") }}
{{ form.college.label }}
{{ form.college(class="form-control form-control-lg") }}
{{ form.section.label }}
{{ form.section(class="form-control form-control-lg") }}
{{ form.email.label }}
{{ form.email(class="form-control form-control-lg") }}
{{ form.submit(class="btn btn-outline-info") }}
</fieldset>
</form>
{% endblock %}
{%extends“base.html”%}
{%block content%}
{{form.hidden_tag()}}
登记册:
{{form.student_number.label}
{{form.student_number(class=“form control form control lg”)}
{{form.last_name.label}
{{form.last_name(class=“form control form control lg”)}
{{form.first_name.label}
{{form.first_name(class=“form control form control lg”)}
{{form.college.label}
{{form.college(class=“form control form control lg”)}
{{form.section.label}
{{form.section(class=“form control form control lg”)}
{{form.email.label}
{{form.email(class=“form control form control lg”)}
{{form.submit(class=“btn btn outline info”)}
{%endblock%}
我还没有试过使用烧瓶。如果我错了,请纠正我。
我认为问题在于RegistrationForm()类下面的这一行:
检查后,wtforms.validators.Length()将验证字符串,但您正在IntegerField()上使用此验证器。尝试改用StringField()。我还没有尝试使用Flask。如果我错了,请纠正我。 我认为问题在于RegistrationForm()类下面的这一行:
检查后,wtforms.validators.Length()将验证字符串,但您正在IntegerField()上使用此验证器。尝试改用StringField()。更改了它,显然它仍然不起作用。虽然在服务器日志中,它说它已进行了后期更改,但显然它仍然不起作用。虽然在服务器日志中显示POST