标签:content 默认 method pre tab welcome 标志位 数据表 imp
pip install flask-sqlalchemy
在hello.py中添加
from flask.ext.sqlalchemy import SQLAlchemy
hello.py中添加
设置数据库链接地址
1 app.config[‘SQLALCHEMY_DATABASE_URI‘] = ‘mysql+mysqldb://<数据库用户名>:<密码>@<数据库地址>/<数据库名称>?charset=utf8‘
1 class User(db.Model): 2 __tablename__ = ‘username‘ 3 id = db.Column(db.Integer,primary_key=True) 4 name = db.Column(db.String(64),unique = True,index= True) 5 6 def __repr__(self): 7 return ‘<User {0}>‘.format(self.name)
1 @app.route(‘/user‘,methods=[‘GET‘,‘POST‘]) 2 def user(): 3 name = None 4 new = False 5 form = NameForm() 6 if form.validate_on_submit(): 7 name = form.name.data 8 form.name.data = ‘‘ 9 if User.query.filter_by(name=name).first() is None: 10 db.session.add(User(name = name)) 11 db.session.commit() 12 new = True 13 return render_template(‘user.html‘,form = form,name = name,new = new)
1 {% block page_content %} 2 <div class="container"> 3 <div class="row"> 4 <div class="col-md-3"> 5 {{ wtf.quick_form(form) }} 6 {% if name %} 7 {% if new %} 8 <h1> Welcome, {{ name }}</h1> 9 {% else %} 10 <h1>See you again, {{ name }}</h1> 11 {% endif %} 12 {% endif %} 13 </div> 14 </div> 15 </div> 16 {% endblock %}
{% if new %}{% else %}{% endif %}:判断new值,为True时显示Welcom <user>,为False时See you again,<user>
Flask-16 Using Flask-SQLAlchemy
标签:content 默认 method pre tab welcome 标志位 数据表 imp
原文地址:http://www.cnblogs.com/rockchen26sh/p/6442371.html