【问题标题】:unable to view my model objects in django using cbv无法使用 cbv 在 django 中查看我的模型对象
【发布时间】:2021-11-01 01:10:46
【问题描述】:

我无法在 django 中查看我的模型项目我做的一切都是正确的,但我的模型项目我没有显示 我的模特

class Quote(models.Model):
    todays_Quote = models.CharField(max_length=500, blank=False)
    by = models.CharField(max_length=100, blank=False)
    created = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.todays_Quote

我的观点

class home(View):
    def get(self, request):
        quote = Quote.objects.all()
        return render(request, 'home.html', {'qoutes':quote})

我的 html 用于渲染它

<section>
       <div class="col-11">
         <div class="text-center">
         {% for Quote in quotes %}
         <h1>{{ Quote.todays_Quote }} hello</h1>
         <p style="float: right;">{{ Quote.by }}</p>
         <p>Todays Date :- <span id="date"></span></p>
         {% endfor %}
        </div>
       </div>
     </section>

知道这有什么问题,是的,我做了迁移

页面来源

</nav>
     <section>
       <div class="col-11">
         <div class="text-center">
         
        </div>
       </div>
     </section> 

【问题讨论】:

  • 您认为print(quote) 显示了什么?
  • 它确实显示了引用 ]>
  • 不知道这里出了什么问题
  • @yedpodtrzitko 我也尝试了函数基础视图,但没有发生任何事情,为什么会发生这种情况我也尝试查看页面源,但该部分变为空白

标签: django django-models django-views django-class-based-views


【解决方案1】:

我的观点

class home(View):
    def get(self, request):
        quote = Quote.objects.all()
        return render(request, 'home.html', {'qoutes':quote}) # <--Here

您的视图中有一个错字“qoutes”。

您将“qoutes”键传递到上下文字典中,因此当您尝试在模板中对其进行迭代时:

 {% for Quote in quotes %}

您的模板找不到“引号”,因为它只知道“qoutes”。

【讨论】:

  • 我的观点中有什么错别字
  • 用更多解释更新了我的评论:)
  • 哦,是的,对不起,我的错
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-18
  • 2021-02-22
  • 2022-07-06
  • 1970-01-01
相关资源
最近更新 更多