python - Django WritingYourFirstDjangoApp Part5
问题描述
我正在使用以下教程网站,尽管我已按照网站上的每条说明进行操作,但还是出现了此错误...
docs.djangoproject.com/en/3.0/intro/tutorial05/
(蟒蛇壳)
In [16]: response = Client.get(reverse('polls:index'))
Internal Server Error: /polls/ Traceback (most recent call last): File "/Users/****/Desktop/Programming1/hello_django/django/django/template/base.py", line 470, in parse
compile_func = self.tags[command] KeyError: 'ls'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/Users/****/Desktop/Programming1/hello_django/django/django/core/handlers/exception.py", line 47, in inner
response = get_response(request) File "/Users/****/Desktop/Programming1/hello_django/django/django/core/handlers/base.py", line 202, in _get_response
response = response.render() File "/Users/****/Desktop/Programming1/hello_django/django/django/template/response.py", line 105, in render
self.content = self.rendered_content File "/Users/****/Desktop/Programming1/hello_django/django/django/template/response.py", line 81, in rendered_content
template = self.resolve_template(self.template_name) File "/Users/****/Desktop/Programming1/hello_django/django/django/template/response.py", line 63, in resolve_template
return select_template(template, using=self.using) File "/Users/****/Desktop/Programming1/hello_django/django/django/template/loader.py", line 42, in select_template
return engine.get_template(template_name) File "/Users/****/Desktop/Programming1/hello_django/django/django/template/backends/django.py", line 34, in get_template
return Template(self.engine.get_template(template_name), self) File "/Users/****/Desktop/Programming1/hello_django/django/django/template/engine.py", line 143, in get_template
template, origin = self.find_template(template_name) File "/Users/****u/Desktop/Programming1/hello_django/django/django/template/engine.py", line 125, in find_template
template = loader.get_template(name, skip=skip) File "/Users/****/Desktop/Programming1/hello_django/django/django/template/loaders/base.py", line 30, in get_template
contents, origin, origin.template_name, self.engine, File "/Users/****/Desktop/Programming1/hello_django/django/django/template/base.py", line 155, in __init__
self.nodelist = self.compile_nodelist() File "/Users/****/Desktop/Programming1/hello_django/django/django/template/base.py", line 193, in compile_nodelist
return parser.parse() File "/Users/****/Desktop/Programming1/hello_django/django/django/template/base.py", line 472, in parse
self.invalid_block_tag(token, command, parse_until) File "/Users/****/Desktop/Programming1/hello_django/django/django/template/base.py", line 534, in invalid_block_tag
"or load this tag?" % (token.lineno, command) django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 1: 'ls'. Did you forget to register or load this tag?
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~/Desktop/Programming1/hello_django/django/django/template/base.py in parse(self, parse_until)
469 try:
--> 470 compile_func = self.tags[command]
471 except KeyError:
KeyError: 'ls'
During handling of the above exception, another exception occurred:
TemplateSyntaxError Traceback (most recent call last) <ipython-input-16-f2b5e9d8990c> in <module>
----> 1 response = Client.get(reverse('polls:index'))
~/Desktop/Programming1/hello_django/django/django/test/client.py in get(self, path, data, follow, secure, **extra)
730 """Request a response from the server using GET."""
731 self.extra = extra
--> 732 response = super().get(path, data=data, secure=secure, **extra)
733 if follow:
734 response = self._handle_redirects(response, data=data, **extra)
~/Desktop/Programming1/hello_django/django/django/test/client.py in get(self, path, data, secure, **extra)
393 return self.generic('GET', path, secure=secure, **{
394 'QUERY_STRING': urlencode(data, doseq=True),
--> 395 **extra,
396 })
397
~/Desktop/Programming1/hello_django/django/django/test/client.py in generic(self, method, path, data, content_type, secure, **extra)
468 query_string = parsed[4].encode().decode('iso-8859-1')
469 r['QUERY_STRING'] = query_string
--> 470 return self.request(**r)
471
472
~/Desktop/Programming1/hello_django/django/django/test/client.py in request(self, **request)
707 got_request_exception.disconnect(dispatch_uid=exception_uid)
708 # Check for signaled exceptions.
--> 709 self.check_exception(response)
710 # Save the client and request that stimulated the response.
711 response.client = self
~/Desktop/Programming1/hello_django/django/django/test/client.py in check_exception(self, response)
569 self.exc_info = None
570 if self.raise_request_exception:
--> 571 raise exc_value
572
573 @property
~/Desktop/Programming1/hello_django/django/django/core/handlers/exception.py in inner(request)
45 def inner(request):
46 try:
---> 47 response = get_response(request)
48 except Exception as exc:
49 response = response_for_exception(request, exc)
~/Desktop/Programming1/hello_django/django/django/core/handlers/base.py in _get_response(self, request)
200 )
201 try:
--> 202 response = response.render()
203 except Exception as e:
204 response = self.process_exception_by_middleware(e, request)
~/Desktop/Programming1/hello_django/django/django/template/response.py in render(self)
103 retval = self
104 if not self._is_rendered:
--> 105 self.content = self.rendered_content
106 for post_callback in self._post_render_callbacks:
107 newretval = post_callback(retval)
~/Desktop/Programming1/hello_django/django/django/template/response.py in rendered_content(self)
79 content explicitly using the value of this property.
80 """
---> 81 template = self.resolve_template(self.template_name)
82 context = self.resolve_context(self.context_data)
83 return template.render(context, self._request)
~/Desktop/Programming1/hello_django/django/django/template/response.py in resolve_template(self, template)
61 """Accept a template object, path-to-template, or list of paths."""
62 if isinstance(template, (list, tuple)):
---> 63 return select_template(template, using=self.using)
64 elif isinstance(template, str):
65 return get_template(template, using=self.using)
~/Desktop/Programming1/hello_django/django/django/template/loader.py in select_template(template_name_list, using)
40 for engine in engines:
41 try:
---> 42 return engine.get_template(template_name)
43 except TemplateDoesNotExist as e:
44 chain.append(e)
~/Desktop/Programming1/hello_django/django/django/template/backends/django.py in get_template(self, template_name)
32 def get_template(self, template_name):
33 try:
---> 34 return Template(self.engine.get_template(template_name), self)
35 except TemplateDoesNotExist as exc:
36 reraise(exc, self)
~/Desktop/Programming1/hello_django/django/django/template/engine.py in get_template(self, template_name)
141 handling template inheritance recursively.
142 """
--> 143 template, origin = self.find_template(template_name)
144 if not hasattr(template, 'render'):
145 # template needs to be compiled
~/Desktop/Programming1/hello_django/django/django/template/engine.py in find_template(self, name, dirs, skip)
123 for loader in self.template_loaders:
124 try:
--> 125 template = loader.get_template(name, skip=skip)
126 return template, template.origin
127 except TemplateDoesNotExist as e:
~/Desktop/Programming1/hello_django/django/django/template/loaders/base.py in get_template(self, template_name, skip)
28 else:
29 return Template(
---> 30 contents, origin, origin.template_name, self.engine,
31 )
32
~/Desktop/Programming1/hello_django/django/django/template/base.py in
__init__(self, template_string, origin, name, engine)
153 self.engine = engine
154 self.source = str(template_string) # May be lazy.
--> 155 self.nodelist = self.compile_nodelist()
156
157 def __iter__(self):
~/Desktop/Programming1/hello_django/django/django/template/base.py in compile_nodelist(self)
191
192 try:
--> 193 return parser.parse()
194 except Exception as e:
195 if self.engine.debug:
~/Desktop/Programming1/hello_django/django/django/template/base.py in parse(self, parse_until)
470 compile_func = self.tags[command]
471 except KeyError:
--> 472 self.invalid_block_tag(token, command, parse_until)
473 # Compile the callback into a node object and add it to
474 # the node list.
~/Desktop/Programming1/hello_django/django/django/template/base.py in invalid_block_tag(self, token, command, parse_until)
532 token,
533 "Invalid block tag on line %d: '%s'. Did you forget to register "
--> 534 "or load this tag?" % (token.lineno, command)
535 )
536
TemplateSyntaxError: Invalid block tag on line 1: 'ls'. Did you forget to register or load this tag?
我有三个 HTML 模板。
detail.html
:
<h1>{{ question.question_text }}</h1>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="{% url 'polls:vote' question.id %}" method="post"> {% csrf_token %} {% for choice in question.choice_set.all %}
<input type='radio' name='choice' id="choice{{ forloop.counter }}" value="{{ choice.id }}">
<label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>{% endfor %}
<input type="submit" value="vote"> </form>
index.html
:
{%ls if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
{% endfor %}
</ul> {% else %}
<p>No polls are available.</p> {% endif %}
results.html
:
<h1>{{ question.question_text }}</h1>
<ul> {% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ achoice.votes }} vote{{ choice.votes|pluralize }}</li> {% endfor %} </ul>
<a href="{% url 'polls:detail' question.id %}">Vote again?</a>
解决方案
您的错误消息指向模板中的错误:
TemplateSyntaxError: Invalid block tag on line 1: 'ls'. Did you forget to register or load this tag?
查看您的index.html
模板,我们看到
{%ls if latest_question_list %}
ls
不是有效的模板标签。去掉它:
{% if latest_question_list %}
推荐阅读
- javascript - 是否有一种惯用的 Javascript 方法可以避免为失败的 `Array.find()` 提供默认值?
- ios - 在具有多个联系人选择的反应本机应用程序中打开默认的 ios 联系人列表
- excel - 识别列表 VBA
- javascript - page.evaluate 和 page.$ 的不同行为具有后续功能
- javascript - 表单只发送一个文件/图像
- javascript - 从数组中获取标志值的按位组合值
- java - 未调用 Java servlet
- magento - Magento 订购电子邮件运费
- javascript - 如何从外部 HTML 文件中获取脚本?
- c - 在 Linux 中使用“watch”运行时获取终端大小