首页 > 解决方案 > Django - 是否可以根据模型字段内的内容添加模板渲染条件?

问题描述

我想使用检测字符串匹配的条件在模板内呈现内容。如果前几个字符串通过条件,那么我想渲染所述对象字段,如果没有,则跳过该对象。

有问题的字段是用于存储博客文本的大型内容字段。此字段具有 HTML 标记,例如<p><iframe>。我希望这个模板渲染检测<iframe>字符串,作为条件。

这是我的代码:

查看.py

def homepage(request, *args, **kwargs):
    obj = blog.objects.filter(posted=True).order_by('-date_posted')

    posts = {
        'object': obj
    }

    return render(request=request, template_name="home.html", context=posts)

html

<div class="container px-5 py-10 mx-auto md:px-20 lg:px-30">
          {% for post in object|slice:":4" %}
          <div class="p-12 md:w-1/2 flex flex-col items-start">
            <span
              class="
                inline-block
                py-1
                px-2
                rounded
                bg-indigo-50
                text-indigo-500 text-xs
                font-medium
                tracking-widest
                font-sans
              "
              >{{ post.subtitle }}</span
            >
            <span class="mt-1 text-gray-500 text-sm font-mono"
              >{{ post.date_posted }}</span
            >
            <a href="articles/{{ post.url_title }}">
              <h2
                class="
                  sm:text-3xl
                  text-2xl
                  title-font
                  font-medium
                  text-gray-900
                  mt-4
                  mb-4
                  font-sans
                "
              >
                {{ post.title }}
              </h2>
            </a>
            <p class="leading-relaxed mb-8 font-sans">
              {{ post.content|truncatechars:300|striptags }}
            </p>

            <div
              class="
                flex
                items-center
                flex-wrap
                pb-4
                mb-4
                border-b-2 border-gray-100
                mt-auto
                w-full
              "
            >
              <a
                href="articles/{{ post.url_title }}"
                class="text-indigo-500 inline-flex items-center font-mono"
                >Read More
                <svg
                  class="w-4 h-4 ml-2"
                  viewBox="0 0 24 24"
                  stroke="currentColor"
                  stroke-width="2"
                  fill="none"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                >
                  <path d="M5 12h14"></path>
                  <path d="M12 5l7 7-7 7"></path>
                </svg>
              </a>
            </div>
            <a class="inline-flex items-center">
              <img
                alt="blog"
                src="https://dummyimage.com/104x104"
                class="
                  w-12
                  h-12
                  rounded-full
                  flex-shrink-0
                  object-cover object-center
                "
              />
              <span class="flex-grow flex flex-col pl-4">
                <span class="title-font font-medium text-gray-900 font-sans"
                  >{{ post.author }}</span
                >
                <span
                  class="text-gray-400 text-xs tracking-widest mt-0.5 font-mono"
                  >Content Writer</span
                >
              </span>
            </a>
          </div>
          {% endfor %}
        </div>

{% for post in object|slice:":4" %}如何自定义此渲染或添加其他可以作为检测条件传递的标签<iframe>

标签: pythonhtmldjangodjango-templates

解决方案


推荐阅读