【问题标题】:Getting keyerror when trying to populate HTML via Django Template尝试通过 Django 模板填充 HTML 时出现 keyerror
【发布时间】:2021-03-16 04:18:59
【问题描述】:

我正在制作一个填充表格的 HTML 模板。 value 中的 show 指的是我的模型 Show,其值为 titlenetworkreleasedesc。这是 HTML:

--根据 Abdul Aziz Barkat 的建议修改--

{% for show in object_list %}
                    <tr>
                        <td>{{ show.id }}</td>
                        <td>{{ show.title }}</td>
                        <td>{{ show.network }}</td>
                        <td>{{ show.release }}</td>
{% endfor %}

我的意见.py:

def shows(request):
    object_list = Show.objects.all()
    context = {'object_list': object_list}
    return render(request, 'shows.html', context)

如上所示,我可以在 shell 中打印代码。但是当试图填充我的模板时,它会抛出一个错误:

Traceback (most recent call last):
  File "C:\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 470, in parse
    compile_func = self.tags[command]
KeyError: 'show.id'

我不确定 base.py -> compile_func = self.tags[command] 的作用。我假设问题来自我的模板和我的views.py之间的错误沟通,但我无法弄清楚。

这是完整的回溯:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\codingdojo\python_stack\django\django_fundamentals\semi_restful_tv\tv_shows_app\views.py", line 7, in shows      
    return render(request, 'shows.html', context)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\shortcuts.py", line 36, in render  
    content = loader.render_to_string(template_name, context, request, using=using)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\loader.py", line 15, in get_template
    return engine.get_template(template_name)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\backends\django.py", line 
34, in get_template
    return Template(self.engine.get_template(template_name), self)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\engine.py", line 143, in get_template
    template, origin = self.find_template(template_name)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\engine.py", line 125, in find_template
    template = loader.get_template(name, skip=skip)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\loaders\base.py", line 29, in get_template
    return Template(
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 156, in __init__
    self.nodelist = self.compile_nodelist()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 194, in compile_nodelist
    return parser.parse()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\defaulttags.py", line 952, in do_if
    nodelist = parser.parse(('elif', 'else', 'endif'))
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 478, in parse
    raise self.error(token, e)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 476, in parse
    compiled_result = compile_func(self, token)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\defaulttags.py", line 811, in do_for
    nodelist_loop = parser.parse(('empty', 'endfor',))
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 472, in parse
    self.invalid_block_tag(token, command, parse_until)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\template\base.py", line 522, in invalid_block_tag
    raise self.error(
django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 26: 'show.id', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line3 
6, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in techi 
cal_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\utils\deprecation.py", line 94, in 
__call__
    response = response or self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\lib\wsgiref\handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\contrib\staticfiles\handlers.py", line 65, in __call__
    return self.application(environ, start_response)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\wsgi.py", line 141, in __call__
    response = self.get_response(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\base.py", line 75, in get_response
    response = self._middleware_chain(request)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 36, in inner
    response = response_for_exception(request, exc)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 90, in response_for_exception
    response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\core\handlers\exception.py", line 125, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 94, in technical_500_response
    html = reporter.get_traceback_html()
  File "C:\codingdojo\python_stack\my_environments\djangoPy3Env\lib\site-packages\django\views\debug.py", line 332, in get_traceback_html
    t = DEBUG_ENGINE.from_string(fh.read())
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 9737: illegal multibyte sequence

这里有一条重要的线可以帮助比我更有知识的人解决问题:

django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 26: 'show.id', expected 'empty' or 'endfor'. Did you forget to register or load this tag?

但由于我可以让 show.id 在 Shell 中打印,我不明白为什么它没有在我的模板中注册。

【问题讨论】:

  • '{% for show in value %}' in each loop is show item is an object?
  • 为什么要暴力破解会话中的所有变量? {% for show in request.session.object_list %} 显然会起作用,但您决定遍历会话中的所有内容并尝试访问其上的值......还有为什么要这样做?为什么不简单地将查询集传递到上下文中?
  • 是的。 request.session = {'object_list': Show.objects.all()} 当前
  • @Abdul Aziz Barkat 我尝试了 request.session.object_list 并出现与上述相同的错误。我会再试一次,让你知道。感谢您的建议
  • @Abdul Aziz Barkat 我运行了它,结果和以前一样,但出现了同样的错误。我想我已经改变了它,因为我已经让 {% for key, value... %} 在另一个程序中工作并想在这个程序中尝试它,然后忘记了它是不太优雅的方式

标签: python django django-models django-views django-templates


【解决方案1】:

会话变量不是因为他们想在模板中呈现变量而使用的东西。 session 变量用于存储当前用户的值,直到他们的会话持续存在。如果我们想存储一些临时值,这特别有用,一些示例用法是:

  • 如果我们允许用户在我们的网站上在明暗模式 (CSS) 之间切换,我们可以在会话中轻松设置他们的首选模式。
  • 想要存储匿名用户购物篮的电子商务网站可以在会话中设置其购物篮的序列化值。

当一个人想要在模板中呈现变量时,应该使用 context 来实现这一目的。 render 快捷函数需要上下文(字典)作为它的第三个参数:

def shows(request):
    object_list = Show.objects.all()
    context = {'object_list': object_list}
    return render(request, 'shows.html', context)

现在在模板中:

{% for show in object_list %}
    <tr>
        <td>{{ show.id }}</td>
        <td>{{ show.title }}</td>
        <td>{{ show.network }}</td>
        <td>{{ show.release }}</td>
    </tr>
{% endfor %}

同时查看您的错误,您的模板中有类似{% show.id %} 的内容,语法{% something %} 用于模板标签。引用变量应该是{{ show.id }}

【讨论】:

  • 感谢您对会话的解释。我更改了 views.py 以匹配,但我仍然返回相同的错误。我将发布完整的回溯和修改后的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
  • 2017-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多