ValueError at /'video_image'属性没有与之关联的文件

时间:2019-11-12 10:45:08

标签: python html django model valueerror

我的模特:

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)

我的观点:

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

我的模板:

       <!-- 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 %}

1 个答案:

答案 0 :(得分:2)

如错误所示,可能有一个News对象没有与之关联的文件。可以通过将blanknull设置为False来使其强制性:

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

或者通过更改以下内容来检查图片是否已设置,然后再访问其网址:

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

收件人:

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

您甚至可以使用以下方式显示某种占位符图像或某些文本:

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