首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何对Django模板隐藏图像?

如何对Django模板隐藏图像?
EN

Stack Overflow用户
提问于 2021-09-06 15:51:36
回答 2查看 263关注 0票数 1

我想做个博客网站。在all post页面上,它呈现数据库中的所有帖子。但并不是所有的帖子都有自己的特色形象。所以,我试图隐藏那些没有任何特征图像的图像部分。

这里是博客/Model.py

代码语言:javascript
运行
复制
class Article(models.Model):
STATUS_CHOICES = (
    ('draft', 'Draft'),
    ('published', 'Published'),
)
author = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
title = models.CharField(max_length=200)
slug = AutoSlugField(populate_from='title', unique=True, null=False, db_index=True)
excerpt = models.CharField(max_length=60)
featured_image = models.ImageField(upload_to="posts", null=True, blank=True, default="default.jpg")
content = FroalaField()
created = models.DateTimeField(auto_now_add=True)
last_update = models.DateTimeField(auto_now=True)
publish = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft')

这里是博客/view.py

代码语言:javascript
运行
复制
# display all blog posts
def posts(request):
    all_posts = Article.published.all()
    context = {'all_posts': all_posts}
    return render(request, 'blog/posts.html', context)


# Single post
def single_post(request, slug):
    post = Article.objects.get(slug=slug)
    context = {
        'post': post,
    }
    return render(request, 'blog/single-post.html', context)

这里是博客/url.py

代码语言:javascript
运行
复制
urlpatterns = [
    path('', views.posts, name="posts"),
    path('post/<slug:slug>/', views.single_post, name="single-post"),
]

这里是post.html

代码语言:javascript
运行
复制
{% for post in all_posts %}
<li class="bg-white px-4 py-6 shadow sm:p-6 sm:rounded-lg">
  <article aria-labelledby="question-title-81614">
    <a href="{{ post.get_absolute_url }}">
      <!-- author details & option menus -->
      <div class="flex space-x-3">
        <div class="flex-shrink-0">
          <!-- author image -->
          <img
            class="h-10 w-10 rounded-full"
            src="{{ post.author.profile.avatar.url }}"
            alt=""
          />
        </div>
        <!-- author name and publish date time -->
        <div class="min-w-0 flex-1">
          <p class="text-sm font-medium text-gray-900">
            <a href="#" class="hover:underline">Dries Vincent</a>
          </p>
          <p class="text-sm text-gray-500">
            <time datetime="{{ post.publish.date }}"
              >{{ post.publish.date }}
            </time>
          </p>
        </div>
      </div>
      <div class="mt-4 space-y-4">
        {# show image if there is a feature image #}

        {% if post.featured_image.url %}

        <!-- article images -->
        <img
          class="object-cover w-full h-64 bg-center rounded-lg"
          src="{{ post.featured_image.url }}"
          alt="{{ post.title }}"
        />

        {% endif %}
      </div>
      <!-- article title -->
      <h1
        id="question-title-81614"
        class="mt-4 text-xl font-medium text-gray-900"
      >
        {{ post.title }}
      </h1>
    </a>
  </article>
</li>
{% endfor %}

以下是更好地理解post.html模板页的图像

EN

回答 2

Stack Overflow用户

发布于 2021-09-06 15:56:45

这看起来更像是错误的默认设置,如果没有提供图像,则应该将其设置为None/NULL

代码语言:javascript
运行
复制
class Article(models.Model):
    # ⋮
    featured_image = models.ImageField(upload_to='posts', null=True, blank=True, default=None)

然后,您可以使用如下条件呈现它:

代码语言:javascript
运行
复制
{% if post.featured_image %}
    <!-- article images -->
    <img
        class="object-cover w-full h-64 bg-center rounded-lg"
        src="{{ post.featured_image.url }}"
        alt="{{ post.title }}"
    />
{% endif %}

您可以使用default.jpgdefault.jpg设置为None,例如在Django shell中使用查询:

代码语言:javascript
运行
复制
Article.objects.filter(
    featured_image='default.jpg'
).update(featured_image=None)
票数 0
EN

Stack Overflow用户

发布于 2021-09-06 16:04:31

在定义模型时,null=TrueBlank=True还将默认值设置为None

如果要呈现那些有featured_picture的Alf,则需要添加一些If post.featured_picture,或者任何您的图像字段的名称,这样就可以了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69077324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档