从首页问答标题到问答详情页
- 主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' -
尝试实现发布评论。
{% extends 'index.html' %} {% block title %}首页{% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="../static/css/index.css"> {% endblock %} {% block main %} <div class="question-box"> <P style="font-size: 18px">{{ user }}prefect</P> <ul class="list-group" style=" padding-left: 10px;padding-right: 10px;"> {% for foo in questions %} <li class="list-group-item"> <span class="glyphicon glyhicon-leaf" aria-hidden="true"></span> <a href="#">{{ foo.username }}</a><br> <a href="{{ url_for('xiangqing',question_id=foo.id )}}">问题:{{ foo.title }}</a><br> <p>详情:{{ foo.detail }}</p> <span class="badge">时间:{{ foo.creat_time }}</span><br> </li> {% endfor %} </ul> </div> {% endblock %}
{% extends 'base.html' %} {% block title %}问答详情{% endblock %} {% block main %} <div class="col-md-2 column "></div> <div class="col-md-8 column "> <div class="page-headr"> <h1>welcome!</h1><br> <h3>Title{{ ques.title }}<br> <small>author:{{ ques.author.username }}<span class="badge">time:{{ ques.creat_time }}</span></small> </h3> <hr> <p>detail:{{ ques.detail }}</p> <hr> <form> <div class="form-group"> <textarea name="new_comment" class="form-control" rows="5" id="new-comment" placeholder="请写下您的观点" style="width: 850px"></textarea><br> </div> <button type="submit" class="btn btn-default" style="width:100px "> 发送 </button> </form> {# <h4>comment:({{ ques.comments|length }})</h4>#} <ul class="list-group" style="margin: 10px"></ul> </div> </div> <div class="col-md-2 column "></div> {% endblock %}
@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('myweb')) @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)
class Comment(db.Model):
__tablename__='comment'
id=db.Column(db.Integer, primary_key=True, autoincrement=True)
author_id = db.Column(db.Integer,db.ForeignKey('user.id'))
question_id = db.Column(db.Integer,db.ForeignKey('question.id'))
creat_time = db.Column(db.DateTime, default=datetime.now)
detail=db.Column(db.Text,nullable=False)
question=db.relationship('Question',backref=db.backref('comments'))
author=db.relationship('User',backref=db.backref('comments'))