- 定义评论的视图函数
@app.route('/comment/',methods=['POST'])
def comment():
读取前端页面数据,保存到数据库中
- 用<input type="hidden" 方法获取前端的"question_id"
- 显示评论次数
- 要求评论前登录
- 尝试实现详情页面下的评论列表显示
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')
@app.route('/comment/', methods=['POST'])
@log
def comment():
comment=request.form.get('new_comment')
ques_id=request.form.get('question_id')
auth_id=User.query.filter(User.username==session.get('user')).first().id
comm=Comment(author_id=auth_id,question_id=ques_id,detail=comment)
db.session.add(comm)
db.session.commit()
return redirect(url_for('detail',question_id=ques_id))
if __name__ == '__main__':
app.run(debug=True)
<!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>
{% extends 'base.html' %}
{% block title %}
问答详情
{% endblock %}
{% block main %}
<div class="box">
<h3 href="#" >{{ ques.title }}</h3>
<small> {{ ques.author.username }}<span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span></small>
<hr>
<p>{{ ques.detail }}</p>
<hr>
<form action="{{ url_for('comment') }}" method="post">
<div><textarea class="form-control" id="comment" rows="3" style="margin-left: 1%" name="comment" placeholder="write your comment"></textarea><br></div>
<input type="hidden" name="question_id" value="{{ ques.id }}">
<button type="submit" >发送</button>
</form>
<h4>评论:({{ ques.comment|length }})</h4>
<ul class="list-unstyled">
{% for foo in comments %}
<li class="list-group-item">
<a>{{ foo.author.username }}</a>
<span class="badge pull-right">{{ foo.create_time }}</span>
<p>{{ foo.detail }}</p>
<br>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
{% block loginhead %}{% endblock %}
{% block registerhead %}{% endblock %}
<p class="text1">哈哈哈<br>
by:厚脸皮羊
</p>
</body>
</html>