Django custom template filter can't work
I have an errror: 'run_target' is not a valid tag library: Template library run_target not found, tried django.templatetags.run_target
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 :)
Below is my file structure:
db/
models.py
templatefilters/
__init__.py
run_target.py
templates/
run.html
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开发者_JAVA技巧 in run_list %}
<tr>
<td>{% dictKeyLookup target_dict run.id %}</td>
</tr>
{% endfor %}
</table>
</form>
{% endblock %}
First thing first: Did you restart your server after making the changes? It's trivial but most people get stuck because of this.
Is db
in your INSTALLED_APPS
setting?
If so, then it looks like the two other things you're missing are:
- an
__init__.py
file in yourdb
folder itself (you've got one in yourtemplatefilters
directory, but not its parent directory) - the
templatefilters
folder should be calledtemplatetags
(see the Code layout section of the documentation).
Incidentally, db
is not a very good name for an app - call it something that more closely identifies what it does.
If everything is done as in the documentation:
- init.py inside app folder, and inside templatetags folder
- app name in the INSTALLED_APPS section from settings.py
Then the problem can be solved restarting the server, if you have coded the module with the new tags/filter during server was running.
精彩评论