Home > front end >  The 'poster' attribute does not have a file associated with it"
The 'poster' attribute does not have a file associated with it"

Time:12-24

the following error occurs "The 'poster' attribute does not have a file associated with it" I don't quite understand what it may be related to and how to fix it I tried to change the url value, but nothing worked. I'm new to django. Please help

html



<div  style="grid-template-columns: repeat(auto-fill, 300px);">
for serial in serials %}
        <div >
            <img src="{{ serial.poster.url }}"  alt="">
                <p>
                    <a href="{% url 'detail_serial' serial.url %}">{{ serial.title }}</a>
                </p>
        </div>
endfor %}
</div>

 views.py

class SerialDetailView(View):
def get(self, request):
serials = Serials.objects.all()
genres = Genre.objects.all()
return render(request, "serials/single_serial.html", {"serials": serials, "genres": genres})


 urls.py


urlpatterns = [
path('register/', views.Register.as_view(), name='register'),
path('reg/', views.Reg.as_view(), name='reg'),
path("serials_list/", views.SerialsView.as_view(), name='serials_list'),
path("add/", views.AddView.as_view(), name='add'),
path("single_serial/", views.SerialDetailView.as_view(), name='single_serial'),
path("<slug:slug>/", views.SingleSerial.as_view(), name='detail_serial'),
path("actor/<int:id>/", views.ActorView.as_view(), name='actor_detail'),
]

CodePudding user response:

My models

class Serials(models.Model):
title = models.CharField('Name',max_length=100)
description = models.CharField('Description', max_length= 200)
poster = models.ImageField('Poster', upload_to='serials/')
date = models.DateField('Date')
country = models.CharField('Страна',max_length=100)
actors = models.ManyToManyField(Actor, verbose_name='actors', related_name='actor')  
genre = models.ManyToManyField(Genre, verbose_name='genres')  
category = models.ForeignKey(Category, verbose_name='category', on_delete=models.SET_NULL, null=True)   
url = models.SlugField(unique=False)
link = models.URLField(max_length=200, blank=True)

CodePudding user response:

Because you are trying to display the image of one of the serials while you did not choose an image for it. First, choose an image for all serials poster or use the following code:

<div  style="grid-template-columns: repeat(auto-fill, 
            300px);">
            for serial in serials %}
                    <div >
                    {% if serial.poster %}
                        <img src="{{ serial.poster.url }}"  
                        alt="">
                    {% endif %}
                            <p>
                                <a href="{% url 'detail_serial' serial.url %}">{{ serial.title }}</a>
                            </p>
                    </div>
            {% endfor %}
</div>
  • Related