我正在制作一个填充表格的HTML模板。value
中的show
引用了我的模型Show
,其值为title
、network
、release
和desc
。以下是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 %}
My views.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?
但是由于我可以在Shell中打印show.id
,我不明白为什么它没有注册到我的模板中
会话变量并不是人们仅仅因为希望在模板中呈现变量而使用的。会话变量用于存储当前用户的值,直到会话持续。例如,如果我们想存储一些临时值,这一点特别有用,一些示例用法如下:
如果要在模板中呈现变量,应使用上下文来实现此目的。
render
快捷方式函数需要上下文(dictionary)作为其第三个参数:现在在模板中:
另外,看看您的错误,您的模板中有类似
{% show.id %}
的东西,语法{% something %}
用于模板标记。对于引用变量,它应该是{{ show.id }}
相关问题 更多 >
编程相关推荐