40 - 个人博客项目-11- 文章详情

1. 新建 templates / article / detail.html 文章详情页面

(1). 继承公共模板

javascript 复制代码
{% extends 'base.html' %}
 
{% block title %}
    文章详情
{% endblock %}

(2). 编写文章样式

css 复制代码
{% block styles %}
    {{ super() }}
    <style>
        #container {
            width: 1200px;
            margin: 0 auto;
            background-color: #83cae3;
            border-radius: 30px;
            padding: 20px;
        }
 
        #container h2 {
            text-align: center;
 
        }
 
        #shang {
 
            margin-top: 20px;
        }
 
        #shang span {
            margin-left: 10px;
        }
 
        #container p {
            overflow: auto;
        }
 
        #left {
            float: left;
        }
 
        #right {
            float: right;
        }
 
        #content {
            margin-top: 60px;
        }
 
        #item_left {
            display: inline-block;
            margin-top: 20px;
 
 
        }
 
        #item_left img {
            width: 60px;
            height: 60px;
            border-radius: 50px;
            margin-left: 100px;
 
        }
 
        #item_right {
            margin-top: 20px;
            display: inline-block;
            float: right;
            clear: both;
            margin-right: 65%;
 
 
        }
    </style>
{% endblock %}

(3). 编写详情页面

html 复制代码
{% block newcontent %}
    <div id="container">
        <div id="detail">
            <h2>{{ article.title }}</h2>
            <div id="shang">
                <div id="left"><span>作者:{{ article.user.username }}</span><span>发布时间:{{ article.pdatetime }}</span>
                </div>
                <div id="right">
                    <span class="glyphicon glyphicon-heart" aria-hidden="true"></span><span
                        tag="0">{{ article.save_num }}</span>
                    <span class="glyphicon glyphicon-thumbs-up"
                          aria-hidden="true"></span><span tag="0">{{ article.love_num }}</span>
                </div>
            </div>
 
            <div id="content">
                {{ article.content|cdecode|safe }}
            </div>
            <hr>
            <div id="comment">
                <p>文章评论:</p>
                <div>
                    <form action="#" method="post">
                        <input type="hidden" name="aid" value="{{ article.id }}">
                        <p>
                            <textarea name="comment" class="form-control" rows="5" cols="60">写下你的评论吧~</textarea>
                        </p>
                        <p><input type="submit" value="评论" class="btn btn-info"></p>
                    </form>
                </div>
            </div>
            <p>
 
            </p>
            <div id="comment_item">
                {% if comments.items %}
                    {% for comment in comments.items %}
                        <div class="item">
                        <div id="item_left">
                            <img src="
                                    {% if comment.user.icon %}{{ url_for('static',filename=comment.user.icon) }}{% else %}{{ url_for('static',filename='images/touxiang.jpg') }}{% endif %}">
                        </div>
                        <div id="item_right">
                            <p>
                                <span>{{ comment.user.username }}</span>
                                <span>{{ comment.cdatetime }}</span>
                            </p>
                            <p>
                                {{ comment.comment }}
                            </p>
                        </div>
 
                        <hr>
                    {% endfor %}
 
                {% else %}
                    <div class="item">
                        当前文章还没有评论,赶快发表意见吧
 
                    </div>
                {% endif %}
 
 
                </div>
                <div id="paginate">
                    <nav aria-label="...">
                        <ul class="pager">
                            <li class="previous {% if not comments.has_prev %}disabled{% endif %}"><a
                                    href="{{ url_for('article.article_detail') }}?page={{ comments.prev_num }}&aid={{ article.id }}"><span
                                    aria-hidden="true">&larr;</span> 上一页</a></li>
 
                            <li class="next {% if not comments.has_next %}disabled{% endif %}"><a
                                    href="{{ url_for('article.article_detail') }}?page={{ comments.next_num }}&aid={{ article.id }}">下一页
                                <span
                                        aria-hidden="true">&rarr;</span></a></li>
                        </ul>
                    </nav>
                </div>
 
 
            </div>
        </div>
    </div>
{% endblock %}

(4). 编写js, 操作文本域点击后,默认内容清空

javascript 复制代码
{% block scripts %}
    {{ super() }}
    <script>
        $(function () {
            //文本域
            $('textarea[name="comment"]').focus(function () {
                $(this).val("")
            })
        })
    </script>
{% endblock %}

2. 编写文章详情函数

python 复制代码
# 文章详情页
@article_bp.route('/detail')
def article_detail():
    # 通过文章id获取文章对象
    article_id = request.args.get('aid')
    article = Article.query.get(article_id)
    # 获取文章分类
    types = Article_type.query.all()
 
    # 登录用户
    user = None
    user_id = session.get("uid", None)
    if user_id:
        user = User.query.get(user_id)
 
    # 单独查询评论
    page = int(request.args.get("page", 1))
    comments = Comment.query.filter(Comment.article_id == article_id).order_by(-Comment.cdatetime).paginate(page=page,
                                                                                                            per_page=5)
    return render_template("article/detail.html", article=article, types=types, user=user, comments=comments)
 
 

3. 配置首页 index.html ,点击标题进入详情

html 复制代码
 <h4><a href="{{ url_for('article.article_detail') }}?aid={{ article.id }}">{{ article.title }}</a></h4>
相关推荐
jay神27 分钟前
基于 Python + Flask + Vue 的校内求职互助平台
前端·vue.js·后端·python·flask·毕业设计
weixin_BYSJ19872 小时前
springboot旅游管理系统04470(附源码+开发文档+部署教程)
java·spring boot·python·算法·django·flask·旅游
编码者卢布2 小时前
【Azure App Service】应用服务(Web App)里的 SNAT 端口 vs 出站连接数:到底是谁限制了谁?
flask·azure·web app
玄米乌龙茶1232 小时前
Web 框架(FastAPI / Flask)核心概念
前端·flask·fastapi
weixin_BYSJ19873 小时前
基于Django的非物质文化遗产管理系统设计与实现(源码 + 文档)98950
java·javascript·spring boot·python·django·flask·php
weixin_BYSJ19873 小时前
springboot鹿邑县旅游网站99312(源码+文档)
java·javascript·spring boot·python·django·flask·php
石工记1 天前
CTO如何落地AI?从0到1的实战路径
人工智能·python·django·flask·numpy·pandas·pyqt
Shan12051 天前
浅谈:无服务器WebSocket解决方案
云原生·flask·serverless
知识分享小能手2 天前
Flask入门学习教程,从入门到精通,Flask智能租房——用户中心知识点详解(9)
python·学习·flask
輕華2 天前
Flask_GET请求与JSON响应实战详解
python·flask·json