制作首页的显示列表。
1. 在首页添加显示问答的列表,并定义好相应的样式。
无序列表
<ul >
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
2. 用字典向index.html传递参数。
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config from datetime import datetime from functools import wraps app = Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class User(db.Model): __tablename__ = '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) db.create_all() 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) creatTime=db.Column(db.DateTime,default=datetime.now) authorID=db.Column(db.Integer,db.ForeignKey('User.id')) author=db.relationship('User',backref=db.backref('question')) db.create_all() @app.route('/') def home(): mycontext={ 'user':'Misaki', } return render_template('shouye.html',**mycontext) @app.route('/sign_in/',methods=['GET','POST']) def sign_in(): if request.method == 'GET': return render_template('denglu.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 session.permanent = True return redirect(url_for('home')) else: return 'password error' else: return 'username is not existed.' @app.route('/sign_up/',methods=['GET','POST']) def sign_up(): if request.method == 'GET': return render_template('zhuce.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: return 'username existed.' else: user1 = User(username=username, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('home')) @app.context_processor def mycontext(): username=session.get('user') if username: return {'username':username} else: return {} @app.route('/logout/') #注销 def logout(): session.clear() return redirect(url_for('home')) def loginFirst(func): @wraps(func) def wrapper(*args,**kwargs): if session.get('user'): return func(*args, **kwargs) else: return redirect(url_for('sign_in')) return wrapper @app.route('/question/',methods=['GET','POST']) @loginFirst def question(): if request.method == 'GET': return render_template('question.html') else: title = request.form.get('title') detail = request.form.get('detail') authorID =User.query.filter(User.username == session.get('user')).first().id user = User.query.filter(User.username == session.get('user')).first() question = Question.query.filter(Question.title == title).first() if question: return 'question existed' else: questions = Question(title=title, detail=detail, authorID=authorID) questions.author = user db.session.add(questions) db.session.commit() return redirect(url_for(('home'))) if __name__ == '__main__': app.run(debug=True)
{% extends'base.html' %} {% block title %} Home {% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for('static',filename='css/shouye.css')}}" type="text/css"> {% endblock %} {% block main %} <body> <ul class="news-list"> <li style="padding-left: 0px;padding-right: 10px;box-shadow: #cccccc" class="news"> <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span> <a href="#">{{ user }}</a><br> <a href="#" class="title">{{ title }}</a> <span class="badge">{{ time }}</span> <p class="detail">{{ detail }}</p> </li> </ul> <ul> <li> <div class="item-one"> <div class="author"> <div class="info"> <a class="nickname" target="_blank" href="/u/ccd1daeaf7c7">Tini</a> <span class="time">6小时前</span> </div> </div> <div class="title"> <a target="_blank" href="/p/f24a71ecb5f5">马克龙访问科特迪瓦获民众热情欢迎 争相握手</a> </div> <div class="abstract"> <p> 当地时间2017年11月30日,非洲国家科特迪瓦首都阿比让,法国总统马克龙访问当地,与科特迪瓦总统瓦塔拉一起出席新地铁线路开通活动。 </p> </div> <div class="meta"> <a class="collection-tag" target="_blank" href="/c/20f7f4031550">社会热点</a> <a target="_blank" href="/p/f24a71ecb5f5"> <span> 浏览: 4462</span> </a> <a target="_blank" href="/p/f24a71ecb5f5#comments"> <span> 评论: 101</span> </a> <span> 点赞: 134</span> </div> </div> </li> </ul> </body> {% endblock %} </html>