python - Django 中的键错误。模板渲染期间

标签 python django django-pagination

抱歉,如果它是菜鸟代码或问题。我正在使用 django-pagination 进行分页,我是这样做的。但这在我的页面上给了我 keyError 此外它提到它在模板渲染期间出错。我在这里做错了什么。我已经成功安装了分页,修改了 settings.py 文件。但我不知道我需要在这里做什么。任何帮助将不胜感激。

 <table class="active_table"  summary="active_user">
    <thead>
     <tr><th>Name</th><th>Mobile Number</th><th>Count</th></tr>
    </thead>
    <tbody id="table_content">
     {% load pagination_tags %}
       {% block content %}
         {% autopaginate response_data 5 %}
           {% for b in response_data %}
              <tr class="table_rows"><td>{{ b.name }}</td><td>{{ b.mobile_number }}</td><td>{{ b.count }}</td></tr>
           {% endfor %}
         {% paginate %}
        {% endblock %}
     </tbody>
  </table>

详细的追溯粘贴在这里http://dpaste.com/919526/

查看代码如下

View .py

@csrf_exempt

def active_user_table(请求,b): 如果 request.method != "GET": 提出Http404

if (b=='4'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name,mobile_number,COUNT(*) as count, created FROM core_user,core_useractivity WHERE core_user.id = core_useractivity.user_id GROUP BY user_id ORDER BY count DESC")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data':response_data})
elif (b=='3'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND MONTH(CAST(created as date)) = MONTH(NOW()) AND YEAR(cast(created as date)) = YEAR(NOW()) GROUP BY user_id ORDER BY count DESC")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
elif (b=='2'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND DATEDIFF(NOW(), created) <= 7 GROUP BY user_id ORDER BY count DESC")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
elif (b=='1'):
         cursor = connection.cursor()
         cursor.execute("SELECT core_user.id, name, mobile_number, COUNT(*) as count, created FROM core_user, core_useractivity WHERE core_user.id = core_useractivity.user_id AND DATE(created) = DATE(NOW())")
         response_data = dictfetchall(cursor)
         return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data})
else:
         raise Http404

抱歉,我现在没有使用 django ORM,但我会在未来使用。

最佳答案

您必须在 render_to_response 调用中添加 context_instance:

return render_to_response("siteadmin/active_user_table.tmpl",{'response_data': response_data}, context_instance=RequestContext(request))

或者您可以在 settings.py 中使用 TEMPLATE_CONTEXT_PROCESSORS 元组。将此字符串“django.core.context_processors.request”添加到上下文处理器,每个 RequestContext 将包含一个变量请求。

关于python - Django 中的键错误。模板渲染期间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14809618/

相关文章:

python - 捕获 asyncore 类中的异常

django - 在 Django 管理站点的多选框中按字母顺序排列项目

python - 可选择检索 Django REST 框架中的相关项目

python - 使用 gmail 和 python 发送邮件时出现 SMTPAuthenticationError

Python 浮点到 32 位有限数字

python - 属性错误 : 'ManyToManyDescriptor' object has no attribute 'all' - django

python - Django休息框架: Paginaion in detail view breaks when default ordering field is nullable

python - django-pagination 可以每页做多个分页吗?

python - 使用 django 分页器显示项目编号。

python - 为什么 `try ... except` 比 `if` 快?