【问题标题】:I am not able to pass dynamic data in html file我无法在 html 文件中传递动态数据
【发布时间】:2020-03-27 01:32:37
【问题描述】:

我想将我的数据库信息呈现为 HTML 页面。我使用了从数据库传递动态数据的方式。但是,它不起作用。结果我看到一个空白页面。

models.py

class Business(models.Model):
    business_name = models.CharField(max_length=50)
    business_services = models.CharField(max_length =50)
    business_location = models.CharField(max_length=200)
    business_description = models.TextField()

    def __str__(self):
        return self.business_name

views.py

def get_data(request):
    business = Business.objects.all()
    b = {'business': business}
    return render(request, "test.html", b)

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', get_data),

    ]

test.html

{{ business.business_name }}
{{ business.services }}

【问题讨论】:

  • 没有错误。
  • business 是一个查询集,而不是一个 Business 对象。您需要在模板中循环它。
  • 我找不到你

标签: python django django-templates django-views


【解决方案1】:

business 是类Business 的实例。因此,在 test.html 中,执行以下操作:

{% for b in business %}
    {{ b.business_name }}
    {{ b.services }}
{% endfor %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多