首页列表显示全部问答,完成问答详情页布局。
Posted 塨槟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了首页列表显示全部问答,完成问答详情页布局。相关的知识,希望对你有一定的参考价值。
1.首页列表显示全部问答:
将数据库查询结果传递到前端页面 Question.query.all()
前端页面循环显示整个列表。
问答排序
2.完成问答详情页布局:
包含问答的全部信息
评论区
以往评论列表显示区。
3.在首页点击问答标题,链接到相应详情页
<ul class="list-group"> <li class="list-group-item" style="width: 800px"> <span class="glyphicon glyphicon-left" aria-hidden="true"></span> <a href="#" target="_blank">{{ user }}</a> <br> <a href="#">{{ title }}</a> <span class="badge">{{time }}</span> <p style="">{{ detail }} </p> </li> {% for foo in questions %} <li class="list-group-item" style="width: 800px"> <span class="glyphicon glyphicon-left" aria-hidden="true"></span> <a href="#" target="_blank">{{foo.author.username }}</a> <br> <a href="{{ url_for(‘detail‘,question_id=foo.id)}}">{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p style="">{{ foo.detail }} </p> {% endfor %} <<!DOCTYPE html>
<html lang="en"> <head> <meta charset="UTF-8"> <title>发布</title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-2 column"> </div> <div class="col-md-6 column"> <p> <h3>标题</h3> </p> <p> </p> <button type="button" class="btn btn-default">发送</button> <h1>评论</h1><br> <ul> <li class="list-group-item">评论1</li> </ul> </div> <div class="col-md-4 column"> </div> </div> </div> </body> from flask import Flask
from flask import request, render_template,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy from functools import wraps from datetime import datetime 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=TabError,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): __table__name=‘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) create_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() # 数据添加测试 # user = User(username =‘vixx123‘,password=‘20120524‘ ) # db.session.add(user) # db.session.commit() # 链接到首页 @app.route(‘/‘) def index(): return render_template(‘index.html‘) # 跳转登陆页面,登陆设置 @app.route(‘/login/‘,methods=[‘GET‘,‘POST‘]) def login(): if request.method==‘GET‘: return render_template(‘login.html‘) else: usern = request.form.get(‘uname‘) passw = request.form.get(‘upass‘) user =User.query.filter(User.username == usern,User.password==passw).first() if user: session[‘user‘] = usern session.permanent = True return redirect(url_for(‘index‘)) else: return u‘password error‘ @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(‘index‘)) # 跳转注册页面,设置注册 @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‘)) 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.route(‘/‘) def index(): context={ } return render_template(‘index.html‘,**context) @app.route(‘/detail/<question_id>‘) def detail(question_id): quest=Question.query.filter(Question.id==question_id).first() return render_template(‘detail.html‘,ques=quest) if __name__ == ‘__main__‘: app.run(debug=True)
以上是关于首页列表显示全部问答,完成问答详情页布局。的主要内容,如果未能解决你的问题,请参考以下文章