python - 自定义模板标签:将其实际放在模板中的什么位置?
问题描述
完成 Django 教程后,我尝试按照“包含标签:https ://docs.djangoproject.com/en/2.2/howto/custom-template-tags/ 部分中的说明进行操作。我不熟悉模板标签的语法,我也无法弄清楚阅读文档。
polls_extra.py :
from django import template
from django.template.loader import get_template
register = template.Library()
@register.inclusion_tag('results.html')
def show_results(poll):
choices = poll.choice_set.all()
return {'choices': choices}
结果.html:
<h1>{{ question.question_text }}</h1>
{% load poll_extras %}
<ul>
{% for choice in question.choice_set.all %}
<li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>
<a href= {% show_results poll %}>Vote again?</a>
然后,当我提交投票选项时,我收到错误:
/polls/1/results/ 处的 AttributeError
“str”对象没有属性“choice_set”
它应该如何实际包含在 html 中?
解决方案
推荐阅读
- google-bigquery - 处理 BigQuery(嵌套表)中的重复项
- java - 使用具有不同比例设置的多个显示器时,AWT GraphicsConfig 存在坐标问题
- vue.js - 将数据传递给在 v-data-table 内部调用的组件
- java - 如何使用 MappedByteBuffer 读取大文件?
- datatable - 需要帮助选择过滤数据表 1.10 的性别,以匹配准确的男性和女性关键字
- python - 输入类型不支持 seaborn heatmap ufunc 'isnan'
- python - 3D nrrd 图像可视化(matplotlib)?
- aws-lambda - 在 AWS Lamba 上创建实体类
- c# - 不同函数的不同数组值
- c# - Windows 生物识别框架:AcquireFocus() 无法正常工作。错误访问被拒绝:0x80070005