当我不在Django博客文章中上传图片时,防止出现ValueError

时间:2019-04-01 07:18:50

标签: python django valueerror

我有这个模型:

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)])

如果我添加博客文章并添加图片,一切都很好。如果我不包含图片并尝试在浏览器中查看帖子,则会出现此错误:

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),但这没什么区别。

我如何可以选择上传图片还是不上传图片而不会出现错误?

其他信息

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 %}

Stck跟踪:

Internal Server Error: /news/2/

回溯(最近通话最近):       文件“ /Users/me/.local/share/virtualenvs/lakelandcc-6nBitmwo/lib/python3.7/site-packages/django/template/base.py”,行829,位于_resolve_lookup中         当前=当前[位]     TypeError:“ 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

2 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:2)

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

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

收件人:

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