【问题标题】:unable display template variables on webpage in Django无法在 Django 的网页上显示模板变量
【发布时间】:2021-02-03 07:03:25
【问题描述】:

我无法将数据库中的数据显示为 HTML。我从序列化程序中得到了命令。

views.py

 material_class = Material.objects.filter(material_class=user_class.user_class)
 data = MaterialSerializer(material_class, many=True)
 content = {'material':data.data}
 # In *data.data* I am getting this [OrderedDict([('id', '123'),('material','456')]), 
 OrderedDict([('id','345'),('material','789')])]
 return render(request, 'dashboard/dashboard.html',content)

dashboard.html

{% load static %}
{% csrf_token %} 
<!DOCTYPE html>
<html lang="en">
  <head>
    {% block content %} 
    {% for a in content %}
        <p>{{ a }}</p>
    {% endfor %}
    {% endblock %}  
</head>
</html>

【问题讨论】:

  • 始终记得跟踪上下文词典中的键名

标签: python html django django-templates


【解决方案1】:

在视图的最后一行试试这个。

return render(request, 'dashboard/dashboard.html', {'content': content})

您必须传递值及其键的字典。你只是传递了 OrderedDict。

【讨论】:

  • 非常感谢。你是神。
猜你喜欢
  • 2014-12-17
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 2013-10-31
  • 2017-05-31
相关资源
最近更新 更多