Skip to main content
 首页 » 编程设计

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

2025年05月04日35lhb25

我有这个模型:

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

堆栈跟踪:

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 

请您参考如下方法:

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

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

到

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