【问题标题】:Have issues getting data to display on my web application在获取数据以显示在我的 Web 应用程序上时遇到问题
【发布时间】:2021-12-31 11:51:49
【问题描述】:

我正在尝试从我的数据库中获取“关于我们”信息到我的网络应用程序,但它没有显示,可能是什么问题...

这是数据库中的代码

class About(models.Model):
    about_us = models.TextField()
    achieve = models.TextField(blank=True)
    image_abt = models.ImageField(upload_to="admin_pics", null=True)

    class Meta:
        verbose_name_plural = "About Us"

    def __str__(self):
        return self.about_us

这里是 Html 代码 `

{% extends 'jtalks/base.html' %}
{% load static %}


{%block content%}
<section id="about-home">
    <h2>About Us</h2>
</section>

<section id="about-container">
    {% for about in abouts %}
    <div class="about-img">
        <img src="{{ about.image_abt.url }}" alt="">
    </div>

    <div class="about-text">
        <h2>Welcome to TechEduca, Enhance your skills with best Online Courses</h2>
        <p>{ about.about_us}</p>

        <div class="about-fe">
            <img src="images/fe1.png" alt="">
            <div class="fe-text">
                <h5>400+ Courses</h5>
                <p>You can start and finish one of these popular courses in under our site</p>
            </div>
        </div>
        <div class="about-fe">
            <img src="images/fe2.png" alt="">
            <div class="fe-text">
                <h5>Lifetime Access</h5>
                <p>You can start and finish one of these popular courses in under our site</p>
            </div>
        </div>

    </div>
    {% endfor %}
</section>
{% endblock %}

网站前端没有显示任何内容。

【问题讨论】:

  • 你能显示渲染模板的视图吗?
  • def about(request): return render(request, 'jtalks/about.html')

标签: django database django-models


【解决方案1】:

感谢分享观点。您忘记将数据传递给您的模板。为此,您必须创建查询集并将其传递到如下所示的字典中。将上下文变量添加到您的渲染方法中,以便您可以访问模板中的数据。

def about(request):
    about = About.objects.all()
    context = {
    'abouts': about,    
    }
    return render(request, 'jtalks/about.html', context)

另外,在您的 html 代码中,我看到 { about.about_us},但您必须使用双花括号:{{ about.about_us }}

【讨论】:

  • 谢谢,这行得通!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 2022-01-24
  • 2013-03-26
  • 2019-09-06
相关资源
最近更新 更多