首页列表显示全部问答,完成问答详情页布局
Posted hskwnbr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了首页列表显示全部问答,完成问答详情页布局相关的知识,希望对你有一定的参考价值。
- 首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
- 完成问答详情页布局:
- 包含问答的全部信息
- 评论区
- 以往评论列表显示区。
- 在首页点击问答标题,链接到相应详情页。
{% extends \'basic.html\' %} {% block title %}登陆{% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'../staticcss/20.css\') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'../staticcss/200.css\') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'../static/css/2000.css\') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for(\'static\',filename=\'../static/css/2000.css\') }}"> <script src="{{url_for(\'static\',filename=\'../static/js/39.js\') }}" type="text/css"></script> {% endblock %} {% block index %} </head> <body class="abc"> <p>{{ user }}context</p> <ul class="list-group"> {% for foo in question %} <li class="list-group-item"> <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span> <a href="#">{{ foo.author.username }}</a> <br> <a href="#"title>{{ foo.title }}</a> <br> <span class="badge">{{ foo.creat_time }}</span> <p style="text-indent: 18px">{{ foo.detail }}</p> </li> </ul> {% endfor %} <div> <div class="img"> <a href={{ url_for(\'login\') }}> <img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4071790924,2914289697&fm=27&gp=0.jpg"> </a> <div class="desc"><a href={{ url_for(\'login\') }}>登陆</a> </div> </div> <div class="img"> <a href={{ url_for(\'rege\') }}> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508478993164&di=21005bc9d7cbc500390e5b62c3b859a7&imgtype=0&src=http%3A%2F%2Fwapfile.desktx.com%2Fpc%2F161122%2Fbigpic%2F5832b76c05a7e.jpg"> </a> <div class="desc"><a href={{ url_for(\'rege\') }}>注册</a> </div> </div> </div> <div class="clearfloat"> <img src="https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=4071790924,2914289697&fm=27&gp=0.jpg"> <img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508478993164&di=21005bc9d7cbc500390e5b62c3b859a7&imgtype=0&src=http%3A%2F%2Fwapfile.desktx.com%2Fpc%2F161122%2Fbigpic%2F5832b76c05a7e.jpg"> </div> </body> </html>{% endblock %}
from flask_sqlalchemy import SQLAlchemy import config from flask import Flask,render_template,request,redirect, url_for,session from functools import wraps from datetime import datetime 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) nickname = db.Column(db.String(20)) 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 = { \'question\':Question.query.all() } return render_template(\'index.html\',**context) @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 session.permanent = True return redirect(url_for(\'index\')) else: return u\'password error\' else: return u\'password is not existed\' @app.route(\'/rege/\',methods=[\'GET\',\'POST\']) def rege(): if request.method ==\'GET\': return render_template(\'rege.html\') else: usern = request.form.get(\'username\') passw = request.form.get(\'password\') nickn = request.form.get(\'nickname\') user = User.query.filter(User.username ==usern).first() if user: return \'username.existed.\' else: user1 = User(username=usern,password=passw,nickname = nickn) db.session.add(user1) db.session.commit() return redirect(url_for(\'login\')) def loginFrist(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\']) @loginFrist 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{} @app.route(\'/logout/\') def logout(): session.clear() return redirect(url_for(\'index\')) if __name__=="__main__": app.run(debug=True)
以上是关于首页列表显示全部问答,完成问答详情页布局的主要内容,如果未能解决你的问题,请参考以下文章