1. 在首页添加显示问答的列表,并定义好相应的样式。
无序列表
<ul >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
{% extends'base.html' %} {% block title %} 首页 {% endblock %}{% block head %} {% endblock %} {% block main %}{% endblock %}
- 用户名:{ { user }}
- 问题:{ { question }}
- 时间:{ { time }}
2. 用字典向index.html传递参数。
from flask import Flask,render_template,request,redirect,url_for,sessionfrom flask_sqlalchemy import SQLAlchemyimport configfrom datetime import datetimefrom functools import wrapsapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __table__name = 'user' id = db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(20),nullable=False) password = db.Column(db.String(20),nullable=False) nickname = db.Column(db.String(50))class Question(db.Model): __tablename__ = 'question' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) detail = db.Column(db.Text, nullable=False) creat_time = db.Column(db.DateTime, default=datetime.now) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', backref=db.backref('question'))#db.create_all()@app.route('/')def index(): context={ 'user':'GuFeng', 'question':'喜欢古风?', 'time':'2017年12月1日' } return render_template('index.html',**context)@app.route('/zhuce/',methods=['GET','POST'])def zhuce(): if request.method =='GET': return render_template('zhuce.html') else: usern = request.form.get('name') teln = request.form.get('password') passw = request.form.get('password1') user = User.query.filter(User.username==usern).first() if user: return 'username existed' else: user= User(username=usern,tel=teln,password=passw) db.session.add(user) db.session.commit() return redirect(url_for('login'))@app.route('/login/', methods=['GET','POST'])def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: if user.password ==password: session['user'] = username return redirect(url_for('index')) else: return u'password error' else: return u'username is not existed'@app.context_processordef mycontext(): usern = session.get('user') if usern: return{ 'username':usern} else: return {}@app.route('/logout')def logout(): session.clear() return redirect(url_for('index'))def loginFirst(func): @wraps(func) def wrapper(*args,**kwargs): if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('login')) return wrapper@app.route('/question/',methods=['GET','POST'])@loginFirstdef question(): if request.method == 'GET': return render_template('question.html') else: title = request.form.get('title') detail = request.form.get('detail') author_id = User.query.filter(User.username == session.get('user')).first().id question = Question(title=title, detail=detail, author_id=author_id) db.session.add(question) db.session.commit() return redirect(url_for('index'))if __name__ == '__main__': app.run(debug=True)View Code