我没有找到在没有templatetags帮助的情况下从查询集中获取关系实例对象的选项,所以我尝试使用for循环中的自定义模板标记,如下所述。但得到这个错误:
File "D:\Neon_ecl\Projects\shof\shof\smapy\views.py", line 276, in menulist
'p' :p
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\loader.py", line 67, in render_to_string
template = get_template(template_name, using=using)
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\loader.py", line 21, in get_template
return engine.get_template(template_name)
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\backends\django.py", line 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\engine.py", line 160, in get_template
template, origin = self.find_template(template_name)
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\engine.py", line 134, in find_template
name, template_dirs=dirs, skip=skip,
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\loaders\base.py", line 44, in get_template
contents, origin, origin.template_name, self.engine,
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\base.py", line 191, in __init__
self.nodelist = self.compile_nodelist()
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\base.py", line 233, in compile_nodelist
return parser.parse()
File "C:\Python27\Scripts\tvapp_env\lib\site-packages\django\template\base.py", line 518, in parse
raise self.error(token, e)
TemplateSyntaxError: Invalid block tag on line 28: 'pdscheck', expected 'empty' or 'endfor'. Did you forget to register or load this tag?
在模板中:
<tbody>{% load smapy_extras %}
{% for obj in pds %}
{% pdscheck czpds obj %}
{{ temppd }}
{% if obj in p %}<tr style="color:red;">{%else%}
<tr style="color:grey;">{%endif%}
<th scope="row">{{ obj.id }}</th>
<td>{{ obj.pd_name }}</td>
<td>{{ obj.volume }}</td>
<td>{{ obj.mrp }} </td>
{% if obj in czpds %}<td><input type=submit value="Disable" style="width:100%" onclick="addPds({{shop.id}},{{heading.id}},'pdctg',{{obj.id}})"></form> </td>
{% else %} <td><input type=submit value="Enable" style="width:100%" onclick="addPds({{shop.id}},{{heading.id}},'pdctg',{{obj.id}})"></form> </td>
{%endif%}
</tr>
{% endfor %}
</tbody>
模板标记文件: 来自django导入模板
register = template.Library()
@register.filter(name='pdscheck')
def pdscheck(czpds, obj):
tempctg = 'pdctg'
temppd = czpds.filter(tempctg=obj)
return {'temppd': temppd}
答案 0 :(得分:4)
您注册了过滤器,而不是标签。