【问题标题】:Django template ValueError The 'image' attribute has no file associated with itDjango 模板 ValueError 'image' 属性没有与之关联的文件
【发布时间】:2016-02-06 00:58:00
【问题描述】:

我将上下文信息发送到视图的这一部分。有一个分页器,适用于第 1-3 页。但是转到第四页它有这个错误。

异常类型:ValueError

异常值:“图像”属性没有与之关联的文件。

django debug as 突出显示模板中的错误

模板渲染时出错

在模板/templates/marketplace/entry_list.html,第91行出错:

“图像”属性没有与之关联的文件。

<a href="{{ e.get_absolute_url }}" title="{{ e.title }}">
    {% if e.picture.url %}
        {% thumbnail e.picture "300x600" as thumb %}
            <img src="{{ thumb.url }}" alt="{{ e.title }}" />
        {% endthumbnail %}
    {% endif %}
</a>

视图发送上下文信息部分如下

get_context_data(self, **kwargs):
    if self.request.mobile:
        self.template_name = 'mobile/buy_n_sell.html'
    
    today = datetime.date.today()
    context = super(EntryList, self).get_context_data(**kwargs)
    context['category'] = self.category
    try:
        children = self.category.get_child_categories()
    except:
        children = None
    context['child_category'] = children
    context['area'] = self.area
    return context

【问题讨论】:

  • 尝试将 {% if e.picture.url %} 更改为 {% if e.picture %}。 “url”是一个只读属性,它在内部调用方法 _require_file() 来检查文件是否存在。因此,如果文件不存在,它将引发 ValueError
  • /templates/marketplace/entry_list.html 的第 91 行是哪一行?

标签: django python-2.7 django-templates


【解决方案1】:

使用此代码。也许它会适合你..

<a href="{{ e.get_absolute_url }}" title="{{ e.title }}">
{% if e.picture.url %}
    {% thumbnail e.picture "300x600" as thumb %}
        <img src="{{ e.thumb.url }}" alt="{{ e.title }}" />
    {% endthumbnail %}
{% endif %}

【讨论】:

  • 你能解释一下它为什么有效吗?我认为与问题中的示例没有区别
猜你喜欢
  • 2023-02-21
  • 2020-01-16
  • 1970-01-01
  • 2022-12-11
  • 2021-02-11
  • 2020-03-08
  • 2020-08-11
  • 1970-01-01
  • 2023-01-16
相关资源
最近更新 更多