Python Django中的键错误。在模板渲染期间

Python Django中的键错误。在模板渲染期间,python,django,django-pagination,Python,Django,Django Pagination,抱歉,如果是noob代码或问题。我正在使用django分页进行分页,我是这样做的。但是这在我的页面上给了我keyError at,而且它提到了在模板呈现过程中的一个错误。我在这里做错了什么。我已成功安装分页,修改了settings.py文件。但是我不知道我需要在这里做什么。任何帮助都将不胜感激 <table class="active_table" summary="active_user"> <thead> <tr><th>

抱歉,如果是noob代码或问题。我正在使用django分页进行分页,我是这样做的。但是这在我的页面上给了我keyError at,而且它提到了在模板呈现过程中的一个错误。我在这里做错了什么。我已成功安装分页,修改了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>
def active_user_表格(请求,b): if request.method!=“获取”: 提高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
if(b=='4'):
cursor=connection.cursor()
cursor.execute(“选择core_user.id、name、mobile_number、COUNT(*)作为计数,从core_user、core_useractivity创建,其中core_user.id=core_useractivity.user_id按用户分组\u id按计数顺序描述”)
响应\u数据=dictfetchall(光标)
返回render_to_response(“siteadmin/active_user_table.tmpl”,{'response_data':response_data})
elif(b=='3'):
cursor=connection.cursor()
cursor.execute(“选择core_user.id、name、mobile_number、COUNT(*)作为COUNT,从core_user、core_useractivity创建,其中core_user.id=core_useractivity.user_id和MONTH(CAST(创建为日期))=MONTH(现在())和YEAR(CAST(创建为日期))=YEAR(现在())按用户id分组按计数说明)
响应\u数据=dictfetchall(光标)
返回render_to_response(“siteadmin/active_user_table.tmpl”,{'response_data':response_data})
elif(b='2'):
cursor=connection.cursor()

cursor.execute(“选择core_user.id、name、mobile_number、COUNT(*)作为COUNT,从core_user、core_useractivity创建,其中core_user.id=core_useractivity.user_id和DATEDIFF(NOW(),created)您必须在render_to_响应调用中添加上下文_实例

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

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

我自己解决了这个问题,但至少感谢ndpu的帮助,我确信除了一些设置问题之外没有其他问题。在这个问题中。阿拉斯代尔提到添加“django.contrib.auth.context\u processors.auth”,以模板\u上下文\u处理器。只需添加它,我就得到了正确的预期值。

适用于使用 仍然面临这个错误,只需将
{'request':request}
添加到上下文变量中

context = { ..., 'request':request}
return render(request, 'templatename.html', context)

我之前也遇到了此错误。我遇到了以下错误: 内部服务器错误:/cancel email/

Internal Server Error: /cancel-email/
Traceback (most recent call last):
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 506, in parse
    compile_func = self.tags[command]
KeyError: 'static'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/var/www/recruiter-new/recruiter/scheduler.py", line 803, in cancelEmail
    return render(request,'scheduler/cancel-email-part.html',{"cancel_email" :EmailDetail})
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/shortcuts.py", line 67, in render
    template_name, context, request=request, using=using)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/loader.py", line 96, in render_to_string
    template = get_template(template_name, using=using)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/loader.py", line 32, in get_template
    return engine.get_template(template_name, dirs)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/backends/django.py", line 40, in get_template
    return Template(self.engine.get_template(template_name, dirs), self)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/engine.py", line 190, in get_template
    template, origin = self.find_template(template_name, dirs)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/engine.py", line 157, in find_template
    name, template_dirs=dirs, skip=skip,
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/loaders/base.py", line 46, in get_template
    contents, origin, origin.template_name, self.engine,
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 189, in __init__
    self.nodelist = self.compile_nodelist()
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 230, in compile_nodelist
    return parser.parse()
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 508, in parse
    self.invalid_block_tag(token, command, parse_until)
  File "/home/kashif/recEnv/lib/python3.6/site-packages/django/template/base.py", line 568, in invalid_block_tag
    "or load this tag?" % (token.lineno, command)
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 74: 'static'. Did you forget to register or load this tag?
[07/Aug/2018 08:43:26] "POST /cancel-email/ HTTP/1.1" 500 20789
我尝试了一些谷歌解决方案,但无法解决,最后再次检查我的代码,发现模板文件上有一个愚蠢的错误

只需添加:

{% load static %}

在模板文件的顶部。

我已将这些内容添加到我的settings.py。这是我添加的-:template\u CONTEXT\u PROCESSORS=(“django.core.CONTEXT\u PROCESSORS.auth”、“django.core.CONTEXT\u PROCESSORS.debug”、“django.core.CONTEXT\u PROCESSORS.i18n”、“django.core.CONTEXT\u PROCESSORS.media”,“django.core.context\u processors.static”、“django.contrib.messages.context\u processors.messages”、“django.core.context\u processors.request”)我想问的是,即使这样,我也需要添加context\u instance=RequestContext(request)对于我的render\u to\u response。这将做什么?我是说这符合什么情况?@JohnDoe,我面临着相同的错误,但我使用
render
快捷方式来呈现我的响应;而不是OP使用的
render\u to\u response
方法。错误的原因仍然是相同的;即模板系统无法找到
请求st
变量。只需在上下文中插入该变量就解决了我的问题。
{% load static %}