You need to enable JavaScript to run this app. Django模型字段值解析:筛选标题以Temp:开头的记录并提取pk传递到模板
优惠活动
大模型
产品
解决方案
定价
更多

Django模型字段值解析:筛选标题以Temp:开头的记录并提取pk传递到模板

解决方案

1. 修改views.py查询逻辑

Django ORM内置的__startswith字段查找方法可直接匹配指定前缀的字段值,我们可以分别查询模板类记录和普通帖子记录,同时用only()方法仅加载需要的字段优化查询性能:

def docpostlistview(request):
    # 查当前登录用户的模板记录:标题以"Temp: "开头,仅返回pk、content、title字段
    template_posts = DocPost.objects.filter(
        user=request.user, 
        title__startswith="Temp: "
    ).only('pk', 'content', 'title')
    # 查当前登录用户的普通帖子记录:排除标题以"Temp: "开头的内容
    normal_posts = DocPost.objects.filter(
        user=request.user
    ).exclude(
        title__startswith="Temp: "
    ).only('pk', 'content', 'title')
    # 两个数据集分别传入模板
    return render(request, 'my_app/docpost_list.html', {
        'template_posts': template_posts,
        'normal_posts': normal_posts
    })

注:如果你的DocPost模型和用户关联的外键字段不是user,请替换为实际的字段名;如果不需要按用户过滤,直接删除user=request.user条件即可。

2. 修改HTML模板,拆分独立循环

原来的模板混用了两类数据的渲染逻辑,现在把模板区域和普通帖子区域分别对应两个数据集循环即可:

<div class="blocks">
<div class="row">
    <div class="col">
        <a href="{% url 'doc-post-create-normal' %}" style="color: white; height: 10px; z-index: -6;">TESTESTESTESTESTESTESTES</a>
    </div>
    <!-- 模板专属渲染区域 -->
    {% if template_posts %}
        {% for post in template_posts %}
            <div class="col">
                <div class="card card_template hoverable">
                    <div class="card-image">
                        <a href="{% url 'use_template' pk=post.pk %}" style="color: black">
                            <p>
                                {{ post.content|safe }}
                            </p>
                        </a>
                    </div>
                    <div class="card-content">
                        <p>
                        </p>
                    </div>
                    <div class="card-action">
                        <div class="bottom_text">
                            {{ post.title }}
                            <div class="bottom_button">
                                <form action="{% url 'use_template' pk=post.pk %}" method="POST">
                                    {% csrf_token %}
                                    <div class="card-body">
                                        <a style="background-color: #2f3d50; border-radius: 18px; height: 30px; position: relative; top: 5px;" href="{% url 'use_template' pk=post.pk %}" class="btn btn-primary"><div style="position: relative; top: -3px;">Create</div></a>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="wrapper">
                    <ul>
                        <li><a class="dots_img"> <img src="{% static 'threedots.png' %}" alt=""></a>
                            <ul>
                                <div class="edit_drop">
                                    <form action="{% url 'doc-post-update' pk=post.pk %}" method="POST">
                                        {% csrf_token %}
                                        <div class="card-body">
                                            <a style="background-color: #2f3d50; border-radius: 18px;" href="{% url 'doc-post-update' pk=post.pk %}" class="btn btn-primary">EDIT</a>
                                        </div>
                                    </form>
                                </div>
                                <div class="delete_drop">
                                    <a style="background-color: #2f3d50; border-radius: 18px;" href="{% url 'delete_post' pk=post.pk %}" class="btn btn-danger">Delete</a>
                                </div>
                            </ul>
                        </li>
                    </ul>
                </div>
            </div>
    {% endfor %}
    <hr class="hline">
    {% else %}
        <div class="no_template">
            <p>Your new templates will appear here!</p>
        </div>
        <hr class="hline-non">
    {% endif %}
</div>
</div>
<!-- 普通帖子专属渲染区域 -->
<div class="blocks2">
<div class="row">
    {% if normal_posts %}
        {% for post in normal_posts %}
            <!-- 这里按你的需求写普通帖子的渲染逻辑,可自定义样式、操作按钮 -->
            <div class="col">
                <div class="card hoverable">
                    <div class="card-image">
                        <p>{{ post.content|safe }}</p>
                    </div>
                    <div class="card-action">
                        {{ post.title }}
                        <!-- 普通帖子的编辑、删除按钮按需添加即可 -->
                    </div>
                </div>
            </div>
        {% endfor %}
    {% else %}
        <div class="no_post">
            <p>Your new documents will appear here!</p>
        </div>
    {% endif %}
</div>
</div>

内容的提问来源于stack exchange,提问作者Russell Hertel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 00:42:01