首页列表显示全部问答,完成问答详情页布局。
Posted xulishuai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了首页列表显示全部问答,完成问答详情页布局。相关的知识,希望对你有一定的参考价值。
- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
- 完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
- 在首页点击问答标题,链接到相应详情页。
{% extends ‘base.html‘ %} {% block title %}首页{% endblock %} {% block head %} <link href="{{ url_for(‘static‘,filename=‘css/index.css‘) }}" rel="stylesheet" type="text/css"> <script src="{{ url_for(‘static‘,filename=‘js/index.js‘) }}"></script> {% endblock %} {% block main %} <p>{{ username }}contextx</p> <ul class="list-group"style=""> {% for foo in questions %} <li class="list-group-item"> <span class="glyphicon glyphicon-leaf"aria-hidden="true"></span> <a href="#">{{ foo.author.username }}</a> <br> <a href="*">{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p style="text-indent:18px">{{ foo.detail }}</p> </li> </ul> <body background="http://f10.baidu.com/it/u=4181379019,968220141&fm=72"></body> {% endblock %}
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy import config from functools import wraps from datetime import datetime app = 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)) 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) 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 base(): return render_template(‘base.html‘) @app.route(‘/index/‘) def index(): context = { ‘question‘:Question.query.all() } return render_template(‘index.html‘,**context) @app.route(‘/detail‘) def detail(): return render_template("detail.html") @app.route(‘/regist/‘,methods=[‘GET‘,‘POST‘]) def regist(): if request.method==‘GET‘: return render_template(‘regist.html‘) else: username=request.form.get(‘username‘) password=request.form.get(‘password‘) nickname=request.form.get(‘nickname‘) user=User.query.filter(User.username==username).first() if user: return u‘username existed‘ else: user=User (username=username,password=password,nickname=nickname) 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.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‘]) @loginFirst def 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‘)) @app.context_processor def mycontext(): usern=session.get(‘user‘) if usern: return{‘username‘:usern} else: return{} if __name__ == ‘__main__‘: app.run(debug=True)
{% extends ‘base.html‘ %} {% block title %} 反馈中心 {% endblock %} {% block main %} <div class="divtouming"> <div class="panel panel-default" style="width: 800px"> <div class="panel-body"> <form class="form-horizontal" role="form"> <div class="form-group"> <label for="fankuititle" class="col-sm-2 control-label">反馈原因</label> <div class="col-sm-10"> <input type="text" class="form-control" id="fankuititle"> </div> </div> <div class="form-group"> <label for="fankuitext" class="col-sm-2 control-label">详情</label> <div class="col-sm-10"> <textarea id="fankuitext" rows="5" cols="87"></textarea> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form> </div> </div></div> {% endblock %}
以上是关于首页列表显示全部问答,完成问答详情页布局。的主要内容,如果未能解决你的问题,请参考以下文章