首页 > 解决方案 > Django 关于不同的模板有不同的结果

问题描述

我是一个超级新手,所以请参考这个并阅读它。

我正在创建一个通过 DB <> API <> WEB 方法进行通信的公告板。

使用comment.html 成功导入评论。但是,无法在 boardapi_detail.html 的 {%inlude%} 处导入评论。

请参考文章底部的图片。

视图.py

class Commentapi_list(generic.TemplateView):
def get(self, request, *args, **kwargs):
    data = {
        'pk': self.kwargs['pk'],
    }

    url = 'http://127.0.0.1:8080/boardapi/'  + str(data['pk']) + '/comment/'
    datas = requests.get(url, params=data).json()

    # print(datas)
    # print(type(datas))

    df = pd.DataFrame(datas)

    clist = [tuple(r) for r in df.to_numpy()]
    print(clist)
    # print(type(clist))

    return render(self.request, 'comment_form.html', {
        'comment_list' : clist
    })

网址.py

path('board/<int:pk>/', views.Boardapi_detail.as_view(), name="board_detail")

评论.html

{% if not comment_list %}
<p class="text-center"> There is no comment. </p>
{% endif %}

<div style="margin:20px 0;">
{% for i in comment_list %}
<table width="100%" cellpadding="0" cellspacing="0" class="tbl_replist" id="reply_list_layer">
    <tbody>
        <tr style="height:45px;border-top:solid 1px #dddddd;">
            <td style="padding-left: 10px;border-top: 1px solid #eee;width: 114px;">{{ i.1 }}</td>
            <td style="vertical-align: top;border-top: 1px solid #eee;padding: 10px 0;">{{ i.2 }}
                <span style="color: #999;font-size: 11px;display: block;">{{ i.3 }}</span>
            </td>
        </tr>
        {% if user.username == i.c_writer or user.is_staff %}
            <a href="/board/{{ i.0 }}/delete/" class="btn btn-outline-success">delete</a>
        {% endif %}
    </tbody>
</table>
{% endfor %}
</div>

boardapi_detail.html

{% if not board_detail %}
                <p class="text-center">There's nothing. </p>
            {% else %}
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr style="height:45px;border-top:solid 1px #dddddd;">
                        <td width="20%" align="center">title</td>
                        <td width="80%" align="left">{{ board_detail.b_title }} </td>
                    </tr>
                    <tr style="height:45px;border-top:solid 1px #dddddd;">
                        <td width="20%" align="center">writer</td>
                        <td width="80%" align="left">{{ board_detail.b_writer }} </td>
                    </tr>
                    <tr style="height:45px;border-top:solid 1px #dddddd;">
                        <td width="20%" align="center">note</td>
                        <td width="80%" align="left">{{ board_detail.b_note }}</td>
                    </tr>
                    <tr style="height:45px;border-top:solid 1px #dddddd;">
                        <td width="20%" align="center">time</td>
                        <td width="80%" align="left">{{ board_detail.b_date }} </td>
                    </tr>
                    <tr style="height:60px;border-top:solid 1px #dddddd;" align="center">
                        <td width="100%" colspan="2">
                            <a href="/board/{{ board_detail.b_no }}/edit/" class="btn btn-outline-success">edit</a>
                            <button class="btn btn-outline-success my-2 my-sm-0" style="margin-right:10px;" onclick="location.href='/board/'">list</button>
                        </td>
                    </tr>
                        <tr style="height:45px;border-top:solid 1px #dddddd;">
                            <td width="20%" align="center">comment</td>
                            <td width="80%" align="left">({{ board.comment_set.all.count }})</td>
                        </tr>
                </table>
                {% endif %}

                {% include "comment.html" %}

下面的图片是关于第一篇文章的。

comment.html 页面 在此处输入图像描述

↑成功导入全部3条评论。

boardapi_detail.html 页面 ↑一条评论都没有。在此处输入图像描述

标签: django

解决方案


推荐阅读