Django: TemplateSyntaxError, Invalid filter

僤鯓⒐⒋嵵緔 提交于 2020-12-30 09:51:07

问题


I'm trying to access a value from a dictionary using a variable, all in an HTML file that follows Django's Templating language. Django's templating language doesn't let you access a dictionary using a variable as a key, so I decided to use Django filters to do it. However, I am getting a "Invalid filter" error and I can't figure out why.

My html file

<table class="table">
<tbody>
    <tr>
        <th scope="row">Username</th>
        <th scope="row">Full Name</th>
        <th scope="row">Points</th>
        {% for subject in subjects %}
        <th scope="row">{{ subject }}</th>
        {% endfor %}
    </tr>
    {% for user in users %}
    <tr>
        <td>
            <a href="{% url 'profile' username=user.username %}">{{ user.username }}</a>
        </td>
        <td>{{ user.first_name }} {{ user.last_name }}</td>
        <td>{{ user.profile.points.total }}</td>
        {% for subject in subjects %}
        <td>{{ user.profile.points|keyvalue:subject }}</td>
        {% endfor %}
    </tr>
    {% endfor %}
</tbody>

My filter.py

from django import template
register = template.Library()

@register.filter
def keyvalue(dict, key):    
    return dict[key]

My error message

TemplateSyntaxError at /leaderboard/
Invalid filter: 'keyvalue'

Thank you for any help :D

EDIT: Pasting my views.py and urls.pu as requested

views.py

def leaderboard(request):
    global leaderboard_public
    users = sorted(User.objects.all(), key=lambda t: t.profile.points['total'], reverse=True)

    if request.method == "POST":
        leaderboard_public = not leaderboard_public
        return redirect(leaderboard)
    subjects = list(Subject.objects.values('subject_name'))
    subjects = [i['subject_name'] for i in subjects]
    print(subjects)
    return render(request, "leaderboard.html", {
        "users": users,
        "subjects": subjects,
        # "leaderboard_public": leaderboard_public,
    })

urls.py

from accounts import views as accountsViews
from puzzles import views as puzzlesViews

urlpatterns = [
url(r'^admin/', admin.site.urls),

url(r'^signup/$', accountsViews.signup_without_email, name="signup"),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
    accountsViews.activate, name='activate'),
url(r'^logout/$', authViews.LogoutView.as_view(), name='logout'),
url(r'^login/$', authViews.LoginView.as_view(template_name='login.html'), name="login"),
# url(r'^puzzles/$', puzzlesViews.puzzles, name="puzzles"),
url(r'^puzzle/(?P<puzzleID>.+)/edit/$', puzzlesViews.editPuzzle, name="editPuzzle"),
url(r'^puzzle/(?P<puzzleID>.+)/$', puzzlesViews.puzzle, name="puzzle"),
url(r'^scheduled/$', puzzlesViews.scheduled, name="scheduled"),
url(r'^closed/$', puzzlesViews.closed, name="closed"),
url(r'^leaderboard/$', accountsViews.leaderboard, name="leaderboard"),
# url(r'^leaderboard/(?P<subject>.+)/$', accountsViews.leaderboard_subject, name="leaderboard"),
# url(r'^submissions/$', puzzlesViews.submissions, name="submissions"),
url(r'^profile/(?P<username>.+)/submissions$', puzzlesViews.submissions, name="submissions"),
url(r'^profile/(?P<username>.+)/edit/$', accountsViews.editProfile, name="editProfile"),
url(r'^profile/(?P<username>.+)/$', accountsViews.profile, name="profile"),
url(r'^create/$', puzzlesViews.create, name="create"),
# url(r'^$', views.home, name="home"),
url(r'^$', puzzlesViews.puzzles, name="puzzles"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Let me know if there's any other information needed.


回答1:


Looks like you forget to load filter.py model into template. To use custom filters you need to put filter.py inside app/templatetags directory (note this directory should include __init__.py) and load it in template using syntax {% load filter %}. See documentation here.




回答2:


I tried {% load crispy_forms_filters %} and it worked for me!




回答3:


Using both Django documentation and other users' answers, here's everything I had to do in order for it to work

  1. Create a folder called templatetags
  2. Create a filter.py file inside it
  • I also created an init.py file, but it's probably not necessary in Python 3+
  1. In setting.py file inside the root folder, I added the following
    INSTALLED_APPS = [
    ...
    'templatetags.filter']

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            **'libraries':{
                'filter': 'templatetags.filter',**
            }
        },
    },
    ]
  1. Inside the new created filter.py, I defined my filter
    import datetime
    from django import template
    
    register = template.Library()
    
    @register.filter(name="my_filter_name")
    def my_filter_name(input):
        output = do_something_with_input(input)
        return date
    
    register.filter('my_filter_name', my_filter_name)
  1. Load the filter (configured in the previous steps) inside my html template
{% extends "base.html" %}

{% load filter %}

...

Obs: Django's documentation seems not to be the best documentation I've seen in a while. In fact, it should contain many more examples and be clearer.

I hope this helps anyone fiddling around Django :)



来源:https://stackoverflow.com/questions/48842889/django-templatesyntaxerror-invalid-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!