python - Django 打印我的查询集

标签 python html django django-queryset

我刚开始使用 Django,我正在尝试打印我的查询集对象。我的模板代码如下:

{% for record in qset  %}
        {{record.first}}, {{record.second}}
{%endfor%}

此外,我的 View 文件包含:

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from updatedb.models import Data
from django.core.urlresolvers import reverse

def index(request):
        return render(request, 'updatedb/data.html')

def submit(request):
        first = request.POST["first"]
        second = request.POST["second"]
        d = Data()
        d.first = first
        d.second = second
        d.save()

        if request.POST.get("submit"):
                qset = Data.objects.all()
                print qset[2].first
                print qset[2].second
                return render(request, 'updatedb/results.html', {'queryset':qset}

在运行服务器并提交我的数据时,我得到一个空白网页。为什么会这样?

最佳答案

将模板中的 qset 更改为 queryset,因为名称 qset 未传递给您的模板。

        {{record.first}}, {{record.second}}
{% endfor %}

顺便说一下,您的括号需要在最后一行用 ) 结束。

关于python - Django 打印我的查询集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26794106/

相关文章:

python - 删除python中变量的特定部分

python - 将 dtype= 对象转换为 dtype ='|S5'

Javascript 函数没有返回值

jquery - 如何让这个 jQuery 函数服从 "document.ready"?

python - 查找 Pandas Dataframe 列中某个区间内值的频率

python - 流遇到 HTTP 错误 : 403 Twitter API, 但我已提升访问权限

php - 用于智能电视应用程序的 HTML5 与 Ajax-CE

html - 图片自动给定边框?

python - “QCombination”对象不可迭代

django - 如何在 Django 管理自定义过滤器中包含自定义模型方法?