python - 如何从数据库中获取所有数据并显示在表格中

标签 python mysql angularjs django

大家好,我正在做一个 python-django(v1.6) 项目,我需要创建一个表,其中包含我数据库中的数据。我已经准备好了我的 templeteview 和模板。 我的看法

class MyView(TemplateView):
    model = Mymodel
    template_name = "home.html"

    def get_context_data(self, *args, **kwargs):
        context = super(MyView, self).get_context_data(*args, **kwargs)
        context['ngapp'] = "Myapp"
        return context

    def get_data(request):
        query_results = Mymodel.objects.objects.all()

我的html

{% extends "base.html" %}

<!--Page heading-->
{% block page_title %}My home{% endblock %}
<!---->

{% block content %}
<div>
    <table class="table">
        <tr>
            <th>Name</th>
        </tr>
        {% for item in query_results %}
            <tr>
                <td> {{ item.name}}</td>
            </tr>
        {% endfor %}
    </table>


</div>
{% endblock %}

根据我的代码,不知道为什么不显示名称。如何从我的数据库中获取数据(特定列)并将其显示在表格中?更好地使用 AngularJS?提前谢谢大家

最佳答案

您没有将查询集传递给模板。像这样尝试:

class MyView(TemplateView):
    model = Mymodel
    template_name = "home.html"

    def get_context_data(self, *args, **kwargs):
        context = super(MyView, self).get_context_data(*args, **kwargs)
        context['ngapp'] = "Myapp"
        context['query_results'] = self.get_data()
        return context

    def get_data(self):
        query_results = self.model.objects.all()
        return query_results

关于python - 如何从数据库中获取所有数据并显示在表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35332635/

相关文章:

javascript - Kendo Grid 按键事件

javascript - AngularJS - JS 中的 HTML - 转义单引号

python - 以类属性作为参数的方法工厂

mysql 5.7 -e 选项在第 1 行出现错误 : unknown command '\u'

Python/Matplotlib -> to_rgba : Invalid rgba arg

php - echo 不起作用,位于 ($row=0)

php - Mysql:这样安全到足以不使用准备好的语句吗?

javascript - Angularjs 多个 HTTP POST 请求导致 net::ERR_INSUFFICIENT_RESOURCES

python - Ubuntu 中的 "failed building wheel for twisted"

python - matplotlib 多个连接到事件处理程序?