当我不在我的 Django 博客中上传图片时防止出现 ValueError post

Prevent a ValueError when I don't upload an image in my Django blog post

我有这个型号:

class News(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    date = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(
        get_user_model(),
        on_delete=models.CASCADE,
    )
    thumb = models.ImageField(blank=True)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('news_detail', args=[str(self.id)])

如果我添加博客 post 并包含图片,一切都很好。如果我不包含图像并尝试在我的浏览器中查看 post,我会收到此错误:

raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'thumb' attribute has no file associated with it.
[01/Apr/2019 08:11:48] "GET /news/ HTTP/1.1" 500 178129

我认为 blank=True 如果我不包含图像,可以防止出现任何错误。我还根据 this 问题尝试了 thumb = models.ImageField(blank=True, null=True),但没有任何区别。

如何在我的博客 post 中选择上传图片或不上传图片而不出现错误?

附加信息

news_detail.html

{% extends 'base.html' %}

{% block content %}
    <div class="news-entry">
        <h2>{{ object.title }}</h2>
            <p>by {{object.author }} | {{ object.date }}</p>
            <p align="center"><img src="{{ object.thumb.url }}" /></p>
            <p>{{ object.body }}</p>
    </div>

    <p><a href="{% url 'news_edit' news.pk %}">Edit</a> |
       <a href="{% url 'news_delete' news.pk %}">Delete</a></p>
    <p>Back to <a href="{% url 'news_list' %}">All News</a>.</p>
{% endblock content %}

堆栈跟踪:

Internal Server Error: /news/2/

回溯(最近调用最后): 文件“/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py”,第 829 行,在 _resolve_lookup 中 当前 = 当前 [位] 类型错误:'ImageFieldFile' 对象不可订阅

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/core/handlers/base.py", line 156, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/core/handlers/base.py", line 154, in _get_response
    response = response.render()
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
    return self.nodelist.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 987, in render
    output = self.filter_expression.resolve(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py", line 837, in _resolve_lookup
    current = getattr(current, bit)
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/db/models/fields/files.py", line 61, in url
    self._require_file()
  File "/Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/db/models/fields/files.py", line 38, in _require_file
    raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)
ValueError: The 'thumb' attribute has no file associated with it.
[01/Apr/2019 08:29:46] "GET /news/2/ HTTP/1.1" 500 160702

调用前请检查图片是否存在url

 <p align="center"><img src="{{ object.thumb.url }}" /></p>

{% if object.thumb %}
 <p align="center"><img src="{{ object.thumb.url }}" /></p>
{% endif %}

在模板文件中添加条件以检查缩略图是否存在。仅当图像存在时才显示图像,否则不显示。更改代码:

<p align="center"><img src="{{ object.thumb.url }}" /></p>

至:

{% if object.thumb %}
    <p align="center"><img src="{{ object.thumb.url }}" /></p>
{% endif %}