gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:27:56 25 4
gpt4 key购买 nike

我有这个模型:

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

关于python - 当我不在我的 Django 博客文章中上传图片时防止出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55449843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com