如何在我的网站上显示存储在数据库中的评论?
How can I show comments stored in db on my website?
我想为我的页面创建评论部分,但不是显示特定 post 的评论,而是显示所有 post 的评论。假设有任何用户对特定 post 发表评论,那么我只希望对 post 发表评论。我得到的是所有 post 的评论。让我给你看代码
schema.sql
CREATE TABLE user(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE post(
id INTEGER PRIMARY KEY AUTOINCREMENT,
author_id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY (author_id) REFERENCES user (id)
);
CREATE TABLE comment(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_name TEXT UNIQUE NOT NULL,
comment TEXT NOT NULL,
FOREIGN KEY (user_name) REFERENCES user (username)
);
blog.py
from flask import(
Blueprint,url_for,render_template,flash,g,redirect,request)
from werkzeug.exceptions import abort
from flaskr.auth import login_required
from flaskr.db import get_db
bp=Blueprint('blog', __name__)
@bp.route('/')
def index():
db=get_db()
posts=db.execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' ORDER BY created DESC'
).fetchall()
comments=db.execute(
'SELECT c.id, comment, created, user_name'
' FROM comment c JOIN user u ON c.user_name = u.username'
' ORDER BY created ASC'
).fetchall()
return render_template('blog/index.html', posts=posts,comments=comments)
@bp.route('/create', methods=('GET', 'POST'))
@login_required
def create():
if request.method=='POST':
title=request.form['title']
body=request.form['body']
error=None
if not title:
error='Title is required'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'INSERT INTO post (title,body,author_id)'
'VALUES (?,?,?)',
(title,body,g.user['id']),
)
db.commit()
return redirect(url_for('blog.index'))
return render_template('blog/create.html')
def get_post(id, check_author=True):
post=get_db().execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?',
(id,),
).fetchone()
if post is None:
abort(404, f"Post id {id} doesn't exist.")
if check_author and post['author_id'] != g.user['id']:
abort(403)
return post
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
@login_required
def update(id):
post=get_post(id)
if request.method=='POST':
title=request.form['title']
body=request.form['body']
error=None
if not title:
error='Title is required'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'UPDATE post SET title = ?, body = ?'
' WHERE id=?',
(title,body,id),
)
db.commit()
return redirect(url_for('blog.index'))
return render_template('blog/update.html', post=post)
@bp.route('/<int:id>/delete', methods=('POST',))
@login_required
def delete(id):
get_post(id)
db=get_db()
db.execute('DELETE FROM post WHERE id=?',(id,))
db.commit()
return redirect(url_for('blog.index'))
@bp.route('/<int:id>/comment', methods=('GET','POST'))
@login_required
def comment(id):
post=get_post(id)
if request.method=='POST':
comment=request.form['comment']
error=None
if not comment:
error='Please type comment and try again.'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'INSERT INTO comment (comment,user_name)'
'VALUES (?,?)',
(comment,g.user['username'],),
)
db.commit()
return redirect(url_for('blog.index'))
return render_template('blog/comment.html')
index.html
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Posts{% endblock %}</h1>
{% if g.user %}
<a class="action" href="{{url_for('blog.create')}}">New</a>
{% endif %}
{% endblock %}
{% block content %}
{% for post in posts %}
<article class="post">
<header>
<div>
<h1>{{ post['title'] }}</h1>
<div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
</div>
{% if g.user['id']==post['author_id'] %}
<a class="action" href="{{url_for('blog.update', id=post['id'])}}">Edit</a>
{% endif %}
{% if g.user %}
<a class="action" href="{{url_for('blog.comment', id=post['id'])}}">Comment</a>
{% endif %}
</header>
<p class="body">{{ post['body'] }}</p>
{% for comment in comments %}
<ul class="comments">
<li><span>{{comment['user_name']}}</span> {{comment['comment']}}</li>
</ul>
{% endfor %}
</article>
{% if not loop.last %}
<hr>
{% endif %}
{% endfor %}
{% endblock %}
评论 table 结构似乎不正确。
评论 table 应将 post_id 作为外键 ID,这将表示此特定评论属于列中提及该 ID 的特定 post。
你需要在这里做三件事:
- 更改注释 table 并在其中添加 post_id 作为外键。
CREATE TABLE comment(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_name TEXT UNIQUE NOT NULL,
comment TEXT NOT NULL,
FOREIGN KEY (user_name) REFERENCES user (username)
FOREIGN KEY (post_id) REFERENCES post (post_id)
);
每当用户发表评论时,发送特定 post 的 post_id 并在评论 table.
中填充 post_id
更改检索查询,在其中获取特定于 post 和用户的评论。
如果你做对了,那么这应该很容易完成。
在这里没有看到任何拦截器。
我想为我的页面创建评论部分,但不是显示特定 post 的评论,而是显示所有 post 的评论。假设有任何用户对特定 post 发表评论,那么我只希望对 post 发表评论。我得到的是所有 post 的评论。让我给你看代码
schema.sql
CREATE TABLE user(
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE post(
id INTEGER PRIMARY KEY AUTOINCREMENT,
author_id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY (author_id) REFERENCES user (id)
);
CREATE TABLE comment(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_name TEXT UNIQUE NOT NULL,
comment TEXT NOT NULL,
FOREIGN KEY (user_name) REFERENCES user (username)
);
blog.py
from flask import(
Blueprint,url_for,render_template,flash,g,redirect,request)
from werkzeug.exceptions import abort
from flaskr.auth import login_required
from flaskr.db import get_db
bp=Blueprint('blog', __name__)
@bp.route('/')
def index():
db=get_db()
posts=db.execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' ORDER BY created DESC'
).fetchall()
comments=db.execute(
'SELECT c.id, comment, created, user_name'
' FROM comment c JOIN user u ON c.user_name = u.username'
' ORDER BY created ASC'
).fetchall()
return render_template('blog/index.html', posts=posts,comments=comments)
@bp.route('/create', methods=('GET', 'POST'))
@login_required
def create():
if request.method=='POST':
title=request.form['title']
body=request.form['body']
error=None
if not title:
error='Title is required'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'INSERT INTO post (title,body,author_id)'
'VALUES (?,?,?)',
(title,body,g.user['id']),
)
db.commit()
return redirect(url_for('blog.index'))
return render_template('blog/create.html')
def get_post(id, check_author=True):
post=get_db().execute(
'SELECT p.id, title, body, created, author_id, username'
' FROM post p JOIN user u ON p.author_id = u.id'
' WHERE p.id = ?',
(id,),
).fetchone()
if post is None:
abort(404, f"Post id {id} doesn't exist.")
if check_author and post['author_id'] != g.user['id']:
abort(403)
return post
@bp.route('/<int:id>/update', methods=('GET', 'POST'))
@login_required
def update(id):
post=get_post(id)
if request.method=='POST':
title=request.form['title']
body=request.form['body']
error=None
if not title:
error='Title is required'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'UPDATE post SET title = ?, body = ?'
' WHERE id=?',
(title,body,id),
)
db.commit()
return redirect(url_for('blog.index'))
return render_template('blog/update.html', post=post)
@bp.route('/<int:id>/delete', methods=('POST',))
@login_required
def delete(id):
get_post(id)
db=get_db()
db.execute('DELETE FROM post WHERE id=?',(id,))
db.commit()
return redirect(url_for('blog.index'))
@bp.route('/<int:id>/comment', methods=('GET','POST'))
@login_required
def comment(id):
post=get_post(id)
if request.method=='POST':
comment=request.form['comment']
error=None
if not comment:
error='Please type comment and try again.'
if error is not None:
flash(error)
else:
db=get_db()
db.execute(
'INSERT INTO comment (comment,user_name)'
'VALUES (?,?)',
(comment,g.user['username'],),
)
db.commit()
return redirect(url_for('blog.index'))
return render_template('blog/comment.html')
index.html
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Posts{% endblock %}</h1>
{% if g.user %}
<a class="action" href="{{url_for('blog.create')}}">New</a>
{% endif %}
{% endblock %}
{% block content %}
{% for post in posts %}
<article class="post">
<header>
<div>
<h1>{{ post['title'] }}</h1>
<div class="about">by {{ post['username'] }} on {{ post['created'].strftime('%Y-%m-%d') }}</div>
</div>
{% if g.user['id']==post['author_id'] %}
<a class="action" href="{{url_for('blog.update', id=post['id'])}}">Edit</a>
{% endif %}
{% if g.user %}
<a class="action" href="{{url_for('blog.comment', id=post['id'])}}">Comment</a>
{% endif %}
</header>
<p class="body">{{ post['body'] }}</p>
{% for comment in comments %}
<ul class="comments">
<li><span>{{comment['user_name']}}</span> {{comment['comment']}}</li>
</ul>
{% endfor %}
</article>
{% if not loop.last %}
<hr>
{% endif %}
{% endfor %}
{% endblock %}
评论 table 结构似乎不正确。
评论 table 应将 post_id 作为外键 ID,这将表示此特定评论属于列中提及该 ID 的特定 post。
你需要在这里做三件事:
- 更改注释 table 并在其中添加 post_id 作为外键。
CREATE TABLE comment(
id INTEGER PRIMARY KEY AUTOINCREMENT,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
user_name TEXT UNIQUE NOT NULL,
comment TEXT NOT NULL,
FOREIGN KEY (user_name) REFERENCES user (username)
FOREIGN KEY (post_id) REFERENCES post (post_id)
);
每当用户发表评论时,发送特定 post 的 post_id 并在评论 table.
中填充 post_id更改检索查询,在其中获取特定于 post 和用户的评论。
如果你做对了,那么这应该很容易完成。 在这里没有看到任何拦截器。