Django. ListView. “image”属性没有关联的文件。

huangapple go评论64阅读模式
英文:

Django. ListView. The image attribute has no file associated with it

问题

我是Django的新手,我尝试使用ListView类显示我的模型的图像,但一直收到错误消息"属性没有与之关联的文件"。

以下是我的代码。

model.py:

from django.db import models

class AboutModel(models.Model):
    person_name = models.CharField(max_length=128, null=True)
    person_position = models.CharField(max_length=128, null=True)
    person_photo = models.ImageField(upload_to='team_pictures', blank=True)

views.py:

from django.views import generic
from . import models

class AboutPage(generic.ListView):
    context_object_name = 'team_list'
    model = models.AboutModel

app的urls.py:

from django.urls import path
from . import views

app_name = 'app_main'

urlpatterns = [
    path('products_page/', views.ProductsPage.as_view(), name='products_page'),
    path('about_page/', views.AboutPage.as_view(), name='about_page'),
    path('contacts_page/', views.ContactsPage.as_view(), name='contacts_page'),
]

project的urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static 
from . import views

urlpatterns = [
    path('', views.HomePage.as_view(), name='index'),
    path("admin/", admin.site.urls),
    path('details/', include('app_main.urls', namespace='app_main')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

html:

<div class='container team_members'>
    {% for member in team_list %}
        <img src={{ member.person_photo.url }}>
        <br>{{ member.person_name }} - {{ member.person_position }}<br>
    {% endfor %}
</div>
英文:

I'm new to Django, and I try to show the images of my model via the ListView class but keep getting the error "attribute has no file associated with it".

Below is my code.

model.py:

from django.db import models

class AboutModel(models.Model):
    person_name = models.CharField(max_length=128, null=True)
    person_position = models.CharField(max_length=128, null=True)
    person_photo = models.ImageField(upload_to=&#39;team_pictures&#39;, blank=True)

views.py:

from django.views import generic
from . import models

class AboutPage(generic.ListView):
    context_object_name = &#39;team_list&#39;
    model = models.AboutModel

app's urls.py:

from django.urls import path
from . import views

app_name = &#39;app_main&#39;

urlpatterns = [
    path(&#39;products_page/&#39;, views.ProductsPage.as_view(), name=&#39;products_page&#39;),
    path(&#39;about_page/&#39;, views.AboutPage.as_view(), name=&#39;about_page&#39;),
    path(&#39;contacts_page/&#39;, views.ContactsPage.as_view(), name=&#39;contacts_page&#39;),
]

project's urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static 
from . import views

urlpatterns = [
    path(&#39;&#39;, views.HomePage.as_view(), name=&#39;index&#39;),
    path(&quot;admin/&quot;, admin.site.urls),
    path(&#39;details/&#39;, include(&#39;app_main.urls&#39;, namespace=&#39;app_main&#39;)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

html:

&lt;div class=&#39;container team_members&#39;&gt;
    {% for member in team_list %}
        &lt;img src={{ member.person_photo.url }}&gt;
        &lt;br&gt;{{ member.person_name }} - {{ member.person_position }}&lt;br&gt;
    {% endfor %}
&lt;/div&gt;

Solutions to similar issues on stackoverflow didn't help as custom function views are used there or the problem itself is different.

答案1

得分: 0

解决方案很简单 - 其中一个用户没有图片:)

英文:

The solution was simple - one of users didn't have a picture:)

huangapple
  • 本文由 发表于 2023年2月23日 23:03:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定