首页列表显示全部问答,完成问答详情页布局
- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
- 完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
- 在首页点击问答标题,链接到相应详情页。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> {% block logintitle %}{% endblock %} {% block registertitle %}{% endblock %} 啦啦啦</title> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/base.css') }}"> <script src ="{{ url_for('static',filename='js/base.js') }}"></script> {% block head %} {% endblock %} </head> <body id="myBody"> <nav> <img id="myOnOff" onclick="mySwitch()" src="https://www.runoob.com/images/pic_bulbon.gif" height="20" width="20px"> <a href="{{ url_for('shouye') }}">首页</a> <a href="{{ url_for('wenda') }}">发布问答</a> <a href="{{ url_for('shouye') }}">听一听</a> <input type="text" name="search"> <button type="submit">看一看</button> <ul class="nav nav-tabs"> <li><a href="{{ url_for('lx') }}">首页</a></li> {% if username %} <a class="right" href="#">{{ username }}</a> <a href="{{ url_for('logout') }}">注销</a> {% else %} <a class="right" href="{{ url_for('login') }}">登录</a> <a href="{{ url_for('zhuce') }}">注册</a> {% endif %} </nav> {% block loginhead %}{% endblock %} {% block registerhead %}{% endblock %} <p class="text1">哈哈哈<br> by:厚脸皮羊 </p> </body> </html>
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config 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() @app.route('/') def base(): return render_template('base.html') @app.route('/shouye/') def shouye(): return render_template('shouye.html') @app.route('/login/',methods=['GET','POST']) def login(): if request.method == 'GET': return render_template('login.html') else: usern = request.form.get('username') passw = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: if user.password == passw: session['user'] = usern return redirect(url_for('shouye')) else: return u'password error' else: return u'username is not existed.' @app.context_processor def mycontext(): usern=session.get('user') if usern: return {'username':usern} else: return {} @app.route('/logout') def logout(): session.clear() return redirect(url_for('shouye')) @app.route('/zhuce/',methods=['GET','POST']) def zhuce(): if request.method == 'GET': return render_template('zhuce.html') else: usern = request.form.get('username') passw = request.form.get('password') user = User.query.filter(User.username == usern).first() if user: return u'username existed' else: user1 = User(username=usern, password=passw) db.session.add(user1) db.session.commit() return redirect(url_for('login')) @app.route('/wenda/') def wenda(): return render_template('wenda.html') @app.route('/') def lx(): context = { 'question':Ques.query.all() } return render_template('lx3.html',**context) @app.route('/detail/') def detail(): # quest=Ques.query.filter(Ques.id == Ques_id).first() return render_template('detail.html') if __name__ == '__main__': app.run(debug=True)