Django自定义模板过滤器无法正常工作 [英] Django custom template filter can't work

查看:546
本文介绍了Django自定义模板过滤器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误:
'run_target'不是有效的标签库:没有找到模板库run_target,尝试django.templatetags.run_target

我不知道为什么它不能工作,即使我添加'db.templatefilters'它不能工作...
任何人可以帮助我吗?谢谢:)

I don't know why it can't work, even i add 'db.templatefilters' it can't work too... Can anyone help me? thank you :)

以下是我的文件结构:

db/
    models.py
    templatefilters/
        __init__.py
        run_target.py
templates/
    run.html

run_target.py

run_target.py

from django import template
register = template.Library()

@register.simple_tag
def dictKeyLookup(the_dict, key):
       return the_dict[key]

run.html

{% extends "index.html" %}
**{% load run_target %}**
{% block content %}
<div style="margin-left:150px; margin-top:10px">
<a href="/home">Home</a> >> <b>run</b>
</div>
<form name="form" method="post">
<br>
<input type="submit" value="Delete" style="margin-left:149px; width:80px; height:30px">
<table border="1"; style="margin-left:150px; border-collapse:collapse;margin-top:10px"; cellpadding="4" borderColor=black>

{% for run in run_list %}
    <tr>
    <td>{% dictKeyLookup target_dict run.id %}</td>
    </tr>
{% endfor %}
</table>
</form>
{% endblock %}


推荐答案

code> db 在您的 INSTALLED_APPS 设置?

Is db in your INSTALLED_APPS setting?

如果是,那么看起来你还缺少以下两件事:

If so, then it looks like the two other things you're missing are:


  • 一个 __ init __。py db 文件夹中的c>文件(您的 templatefilters 目录中有一个,但不是其父目录)

  • templatefilters 文件夹应该称为 templatetags (见代码布局部分。 li>
  • an __init__.py file in your db folder itself (you've got one in your templatefilters directory, but not its parent directory)
  • the templatefilters folder should be called templatetags (see the Code layout section of the documentation).

顺便提一句, db 不是应用程序的非常好的名称 - 这更紧密地标识了 它的作用。

Incidentally, db is not a very good name for an app - call it something that more closely identifies what it does.

这篇关于Django自定义模板过滤器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆