英文:
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='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's 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'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('', 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>
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:)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论