Search code examples
pythonhtmldjangomodelvalueerror

ValueError at / The 'video_image' attribute has no file associated with it


my model:

class News(models.Model):
    CATEGORY=(("0","Politics"),("1","Sports"),("2","Health"),("3","Business"),("4","International"),("5","Finance"))
    title=models.CharField(max_length=250)
    story= models.TextField()
    count= models.IntegerField(default=0)
    like = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True , related_name='post_likes')
    video_url = models.URLField(max_length=270,null=True,blank=True)  #makemigrations garna baki xa
    category= models.CharField(choices=CATEGORY, max_length=2)
    slug=models.SlugField(max_length=270,blank=True,null=True)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now=True)
    cover_image=models.ImageField(upload_to="uploads")
    author= models.CharField(max_length=100,null=True)
    video_image = models.ImageField(upload_to="uploads",blank=True,null=True)
    video_title = models.CharField(max_length=250,blank=True,null=True)

my view:

class NewsTemplateView(TemplateView):
    template_name="index.html"


    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        news=News.objects.all()
         context["latest_news"] = news.order_by("-created_at") [:4]
        context["breaking_news"] = news.filter(Q(category="0")|Q(category="1")).order_by("-created_at") [:3]
        context["political_news"] = news.filter(category="0").order_by("-created_at") [:4]
        context["sports_news"] = news.filter(category="1").order_by("-created_at") [:4]
        context["health_news"] = news.filter(category="2").order_by("-created_at") [:4]
        context["business_news"] = news.filter(category="3").order_by("-created_at") [:4]
        context["international_news"] = news.filter(category="4").order_by("-created_at") [:4]
        context["finance_news"] = news.filter(category="5").order_by("-created_at") [:4]
        context["video_news"] = news.order_by("-created_at") [:3]
        context["popular_news"] = news.order_by("-count")[:6]

        return context

my template:

       <!-- Single Video Post -->
            {% for news in video_news %}
            <div class="col-12 col-sm-6 col-md-4">
                <div class="single-video-post">
                    <img src="{{ news.video_image.url }}" alt="">
                    <!-- Video Button -->
                    <div class="videobtn">
                        <a href="{{news.video_url}}" class="videoPlayer"><i class="fa fa-play" aria-hidden="true"></i></a>
                    </div>
                </div>
            </div>
            {% endfor %}

Solution

  • As the error states, there is probably a News object that has no file associated with it. Either make it mandatory by setting blank and null to False:

    video_image = models.ImageField(upload_to="uploads",blank=False ,null=False)
    

    Or check if the image is set before accessing its url by changing:

    <img src="{{ news.video_image.url }}" alt="">
    

    to:

    {% if news.video_image %}
        <img src="{{ news.video_image.url }}" alt="">
    {% endif %}
    

    You can even display some kind of placeholder image, or some text with:

    {% if news.video_image %}
        <img src="{{ news.video_image.url }}" alt="">
    {% else %}
        <img src="link/to/some/placeholder.jpg"
    {% endif %}