输入值后,Python烧瓶表单返回“无”

时间:2020-04-22 15:11:15

标签: python html database forms flask

这是我的第一个python应用程序,我正在关注本教程。 https://scotch.io/tutorials/build-a-crud-web-app-with-python-and-flask-part-one

我似乎对我在表格上输入的信息有问题,并显示在网页上,除“课程”和“讲师”外,所有字段均正常运行。 我试图更改表/模型,但这没有用。 这是我的views.py页面的一部分

@admin.route('/tasterdays/add', methods=['GET', 'POST'])
@login_required
def add_tasterdays():
"""
Add a Tasterday to the database
"""
check_admin()

add_tasterdays = True

form = TasterDayForm()
if form.validate_on_submit():
    tasterdays = TasterDay(name=form.name.data,
                            description=form.description.data,
                           date=form.date.data,
                           course=form.course.data,
                           lecturer=form.lecturer.data)

 try:
        # add tasterday to the database
        db.session.add(tasterdays)
        db.session.commit()
        flash('You have successfully added a new Taster Day.')
 except:
        # in case tasterday  already exists
        flash('Error: Taster Day already exists.')

 # redirect to tasterdays page
    return redirect(url_for('admin.list_tasterdays'))

   # load tasterdays template
   return render_template('admin/tasterdays/tasterday.html', action="Add",
                       add_tasterdays=add_tasterdays, form=form,
                       title="Add Taster Day")

当我从course=form.course.data, lecturer=form.lecturer.data中删除if form.validate_on_submit():时,表单将提交,但表格中的这些部分将填充“ none”。当我保留这些内容时,表格显示该品尝会已经存在,即使不存在。 这是我的表/模型的3/4:

class Course(db.Model):
"""
Create a courses table
"""

__tablename__ = 'courses'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60), unique=True)
description = db.Column(db.String(200))
students = db.relationship('Student', backref='Course',
                            lazy='dynamic')

def __repr__(self):
    return '<Course: {}>'.format(self.name)

class TasterDay(db.Model):
"""
Create a tasterdays table
"""

__tablename__ = 'tasterdays'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60), unique=True)
description = db.Column(db.String(200))
date = db.Column(db.Date, index=True)
course = db.Column(db.String(60), index=True)
lecturer = db.Column(db.String(60), index=True)
students = db.relationship('Student', backref='tasterdays',
                            lazy='dynamic')

def __repr__(self):
    return '<tasterdays: {}>'.format(self.name)

class Lecturer(db.Model):
"""
Create a lecturer table
"""

__tablename__ = 'lecturers'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60), unique=True)
course_id = db.Column(db.Integer, db.ForeignKey('courses.id'))
taster_day_id = db.Column(db.Integer, db.ForeignKey('tasterdays.id'))

def __repr__(self):
    return '<Lecturer: {}>'.format(self.name)

这是正在填写的表格:

class TasterDayForm(FlaskForm):
"""
Form for admin to add or edit a taster day
"""
name = StringField('Name', validators=[DataRequired()])
date = DateField('Date (DD-MM-YYYY)', format='%d-%m-%Y')
course = StringField('Course', validators=[DataRequired()])
lecturer = StringField('Lecturer', validators=[DataRequired()])
description = StringField('Description', validators=[DataRequired()])
submit = SubmitField('Submit')

以下是表单的html:

            <div class="center">
        <table class="table table-striped table-bordered">
          <thead>
            <tr>
              <th width="15%"> Name </th>
              <th width="40%"> Description </th>
                <th width="40%"> Date </th>
                <th width="40%"> Course </th>
                <th width="40%"> Lecturer </th>
              <th width="15%"> Student Count </th>
              <th width="15%"> Edit </th>
              <th width="15%"> Delete </th>
            </tr>
          </thead>
          <tbody>
          {% for tasterdays in tasterdays %}
            <tr>

                 <td> {{ tasterdays.name }} </td>
                 <td> {{ tasterdays.description }} </td>
                 <td> {{ tasterdays.date }} </td>
                 <td> {{ tasterdays.course }} </td>
                 <td> {{ tasterdays.lecturer }} </td>
              <td>
               {% if tasterdays.students %}
                  {{ tasterdays.students.count() }}
                {% else %}
                  0
                {% endif %}
              </td>
              <td>
                <a href="{{ url_for('admin.edit_tasterdays', id=tasterdays.id) }}">
                  <i class="fa fa-pencil"></i> Edit
                </a>
              </td>
              <td>
                <a href="{{ url_for('admin.delete_tasterdays', id=tasterdays.id) }}">
                  <i class="fa fa-trash"></i> Delete
                </a>
              </td>
            </tr>
          {% endfor %}
          </tbody>
        </table>
      </div>

我希望能够同时填充讲师和课程领域,并在我按下“提交”后将其显示出来。

请让我知道是否需要更多信息。

1 个答案:

答案 0 :(得分:0)

设法自己解决了这个问题。

从tastedays表的课程和讲师中都删除了“ index = True”,并且已成功填充字段。