Day 27 实作 user_bp (5)

前言

今天会完成 user_bp,也就是要完成看贴文跟留言的部分。

/posts

首先来看看可以看到全部人贴文的 /posts,他非常简单,所以就不分别了,直接把 HTML 和路径都放出来。他没有资料库的部分,因为我们会直接用之前的 get_posts

posts.html

{% extends "base.html" %}

{% block title %}Posts{% endblock %}

{% block content %}
<div>
    {% for post in posts %}
    <div>
        {{ post['author_id'] }}
        <a href="/post/{{ post['id'] }}">{{ post['title'] }}</a>
        {{ post['description'] }}
        {{ post['time'] }}
    </div>
    {% endfor %}
</div>
{% endblock %}

views.py


@user_bp.route("/posts", methods=["GET"])
@login_required
def all_posts_page():
    posts = get_posts()
    return render_template("posts.html", posts=posts)

基本上它的 HTML 跟 dashboard 是一样的,只是删掉了编辑和删除的连结,也移掉了筛选器的表单。

路径的部分就更简单了,就如同刚刚所说的用 get_posts,然後不带任何筛选器直接抓全部,再丢到 HTML 处理,而处理方式就跟昨天 dashboard 一模一样。

/@username/posts

接下来我们先看看使用者底下的贴文,基本上跟刚刚一样,但要稍微改一下资料库,HTML 也是用一样的,路径改一点点就好。

helper.py

def get_user_by_username(username):
    user = Users.query.filter_by(username=username).first()
    return render_user_data(user.id)

views.py


@user_bp.route("/@<username>/posts", methods=["GET"])
@login_required
def user_posts_page(username):
    user = get_user_by_username(username)
    posts = get_posts(user["id"])
    return render_template("posts.html", posts=posts)

基本上就是用使用者名称把他整个物件都捞出来,然後当成 get_posts 的筛选器。

/post/

接下来看看浏览贴文的介绍,我们这次真的要从资料库开始写起。

def add_comment(user_id, post_id, content):
    comment = Comments(user_id, post_id, content)
    db.session.add(comment)
    db.session.commit()

def get_comments(post_id):
    comments = Comments.query.filter_by(post_id=post_id).all()
    return [
        {
            "id": comment.id,
            "author_id": comment.author_id,
            "post_id": comment.post_id,
            "content": comment.content,
        }
        for comment in comments
    ]

我们需要新增留言和得到留言,但我们这次就不多验证了,抓留言的部分要转成 dict。

接下来轮到 HTML,在这里面需要放蛮多东西的。

post.html

{% extends "base.html" %}

{% block title %}{{ post['title'] }}{% endblock %}

{% block content %}
<form action="/post/{{ post['id'] }}" method="post">
    {{ form.csrf_token }}
    {{ form.content }}
    {{ form.submit }}
</form>
<div>
    <div>
        <div>{{ post['author_id'] }}</div>
        <div>{{ post['title'] }}</div>
        <div>{{ post['description'] }}</div>
        <div>{{ post['time'] }}</div>
        <div>
            {{ post['content'] | markdown }}
        </div>
    </div>
</div>
<div>
{% for comment in comments %}
    {{ comment['author_id'] }}
    {{ comment['content'] }}
{% endfor %}
</div>
{% endblock %}

里面有一个表单,就是之前写的新增留言的表单,接下来放的是贴文内容,比较特别的是我们终於用到 | markdown 了,这是 Flask-Markdown 的功劳,最後我们放了一堆 comments,就是从刚刚 get_comments 拿到的。

最後我们来到路径本身。


@user_bp.route("/post/<int:post_id>", methods=["GET", "POST"])
@login_required
def view_post_page(post_id):
    form = AddCommentForm()
    if request.method == "GET":
        post = render_post(post_id)
        comments = get_comments(post_id)
        return render_template("post.html", post=post, comments=comments, form=form)
    if request.method == "POST":
        if form.validate_on_submit():
            add_comment(current_user.id, post_id, form.content.data)
            flash("Comment added.")
        else:
            for _, errors in form.errors.items():
                for error in errors:
                    flash(error, category="alert")
        return redirect(url_for("user.view_post_page", post_id=post_id))

基本上就是类似的程序码,但这次贴文跟留言都要抓到。

/comments

在最後,我们来到了留言的 dashboard。

def get_user_comments(user_id):
    comments = Comments.query.filter_by(author_id=user_id).all()
    return [
        {
            "id": comment.id,
            "author_id": comment.author_id,
            "post_id": comment.post_id,
            "content": comment.content,
        }
        for comment in comments
    ]

他非常明显跟刚刚的 get_comments 很接近,几乎是复制贴上,只是改成了 author_id

接下来是 HTML 的部分。

comments.html


{% extends "base.html" %}

{% block title %}comments{% endblock %}

{% block content %}
<div>
{% for comment in comments %}
    {{ comment['author_id'] }}
    {{ comment['post_id'] }}
    {{ comment['content'] }}
{% endfor %}
</div>
{% endblock %}

基本上他跟刚刚最下面差不多,只是加上了 comment['post_id'],也可以在这边加个连结连到那则贴文去。

@user_bp.route("/comments", methods=["GET"])
@login_required
def comments_dashboard_page():
    comments = get_user_comments(current_user.id)
    return render_template("comments.html", comments=comments)

最後来到路径的部分,非常简单,就是把留言抓出来然後丢给 HTML 处理好,就结束了。


<<:  规划 Parser 的测试

>>:  【Day13】:EXTI外部中断/事件控制器

Day 30:结束後的下一步

回顾 & 总结 今天是最後一天了,来回顾一下这些日子分享及学习的内容,虽然是写LeetCod...

Ruby幼幼班--Rotate String

坚持传教K-pop...就可以坚持每天解题?? Rotate String 题目连结:https:...

总结与心得

经过漫长的环境设定与软件安装後 终於把 SQL Server Failover Cluster 基本...

Day22:【技术篇】CSS 的变数运用技巧(1)

一、前言   在 JavaScript 的世界里有变数,那初学者们也知道 CSS (阶层式样式表,C...

第12天~

继续开工~intent 的搜寻关键字-android intent action https://d...