我正在使用Flask建立一个网站。下面显示了app.py,html模板和db(postgresql)数据。点击URL http://127.0.0.1:5000/时,我们可以输入用户名和电子邮件。 http://127.0.0.1:5000/users将显示在db中更新的内容,这里是Mak mak@mk.com。
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'] =
'postgres://postgres:123@localhost/sample'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80))
email = db.Column(db.String(100), unique = True)
def __init__(self, username, email):
self.username = username
self.email = email
def __repr__(self):
return '<User %r>' %self.username
@app.route('/')
#def index():
def index():
return render_template('add_user.html')
@app.route('/post_user', methods=['POST'])
def post_user():
user = User(request.form['username'], request.form['email'])
db.session.add(user)
db.session.commit()
return redirect(url_for('index'))
@app.route('/users')
def get_users():
users = User.query.all()
return render_template('get_user.html', user = users)
@app.route('/homepage')
def display_homepage():
return 'Display homepage'
if __name__ =='__main__':
app.run()
get_user.html
{% for singleuser in users %}
<ul>
<li>singleuser.username</li>
</ul>
{% endfor %}
Sql模式详细信息 1个“ Mak”“ mak@mak.com”
答案 0 :(得分:1)
在get_users ()
中,return语句中有错字
return render_template('get_user.html', users = users)