简体   繁体   English

sqlite数据库中的烧瓶Sqlalchemy数据未显示

[英]Flask Sqlalchemy data from sqlite database not shown

I'm learning to use a database in flask at https://www.tutorialspoint.com/flask/flask_sqlalchemy.htm and my problem is that data doesn't show up on html! 我正在https://www.tutorialspoint.com/flask/flask_sqlalchemy.htm学习在烧瓶中使用数据库,而我的问题是数据没有显示在html上!
My code : 我的代码:

from flask import Flask, redirect, url_for, request, render_template, session, escape, flash
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(db.Integer, primary_key=True)
  stu_num = db.Column('Student Number', db.Integer)
  stu_name = db.Column('Name', db.String(100))

  def __init__(self, number, name):
    self.num = number
    self.name = name

@app.route('/students/')
def show_students():
  return render_template('show_students.html', students=Students.query.all())

@app.route('/addstudents/', methods=['POST', 'GET'])
def add_students():
  if request.method == 'POST':
    if not request.form['stu_num'] or not request.form['stu_name']:
      flash('Please fill in all the fields')
    else:
      student = Students(request.form['stu_num'], request.form['stu_name'])
      db.session.add(student)
      db.session.commit()
      flash('Record added')
      return redirect(url_for('show_students'))
  return render_template('add_students.html')

if __name__ == '__main__':
  db.create_all()
  app.run(debug = True)

show_students.html: show_students.html:

<!doctype>
  <html>
    <body>

      {% for message in get_flashed_messages() %}
        {{ message }}
      {% endfor %}

      <h2><a href="{{ url_for('add_students') }}">Add Students</a></h2>

     <table>
       <tr>
         <th>Id</th>
         <th>Student Number</th>
         <th>Name</th>
       </tr>

         {% for student in students %}
         <tr>
           <td>{{ student.id }}</td>
           <td>{{ student.num }}</td>
           <td>{{ student.name }}</td>
         </tr>
         {% endfor %}
     </table>
   </body>
 </html>

add_students.html: add_students.html:

<!doctype>
  <html>
    <body>

      {% for message in get_flashed_messages() %}
        {{ message }}
      {% endfor %}

      <form action="http://localhost:5000/addstudents/" method="post">
       <p>Student Number: <input type="text" name="stu_num"></p>
       <p>Student Name: <input type="text" name="stu_name"></p>
       <p><input type="submit" value="Submit"></p>
      </form>

   </body>
 </html>

When I add data to the database, Id, which I set to auto-increment shows up, but the rest of data isn't shown. 当我向数据库中添加数据时,会显示设置为自动增量的ID,但不会显示其余数据。

SQLAlchemy provides an __init__ function for you which takes care of creating new objects, initializing fields, database, etc. By writing your own __init__, you are overriding SQLAlchemy's __init__(), and preventing it from doing its thing! SQLAlchemy为您提供了__init__函数,该函数负责创建新对象,初始化字段,数据库等。通过编写自己的__init__,您将覆盖SQLAlchemy的__init __()并阻止其执行其操作!

So try getting rid of your __init__ function! 因此,请尝试摆脱__init__函数!

(Or if there's stuff you want to do in __init__, then make sure to call super().__init__ before or after doing whatever it is that you want to do.) (或者,如果您想在__init__中进行某些操作,请确保在执行您想做的任何事情之前或之后调用super().__ init__。)

(But to start, just try getting rid of it...) (但是首先,请尝试摆脱它...)

You are using inconsistent variable names here in init function, use 您在init函数中使用的变量名称不一致,请使用

def __init__(self, number, name):
    student.stu_num=number
    student.stu_name=name

in show_students.html try using 在show_students.html中尝试使用

<td>{{ student.stu_num }}</td>
<td>{{ student.stu_name }} </td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM