从首页问答标题到问答详情页
Posted 20150605096谢阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从首页问答标题到问答详情页相关的知识,希望对你有一定的参考价值。
- 主PY文件写视图函数,带id参数。
@app.route(‘/detail/<question_id>‘)
def detail(question_id):
quest =
return render_template(‘detail.html‘, ques = quest) - 首页标题的标签做带参数的链接。
{{ url_for(‘detail‘,question_id = foo.id) }} - 在详情页将数据的显示在恰当的位置。
{{ ques.title}}
{{ ques.id }}{{ ques.creat_time }}{{ ques.author.username }}
{{ ques.detail }} -
建立评论的对象关系映射:
class Comment(db.Model):
__tablename__=‘comment‘ -
尝试实现发布评论。
<!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)
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)
以上是关于从首页问答标题到问答详情页的主要内容,如果未能解决你的问题,请参考以下文章