0

I want to register a simple filter in Django in order to concat two strings in a template. I found many questions about this here, but seemingly no answer solves my problem.

settings.py

'MedAbrDirk.apps.MedabrdirkConfig',
'MedAbrDirk.templatetags',

MedAbrDirk/templatetags/my_tags.py

from django import template
register = template.Library()

@register.filter
def kette(arg1,arg2):
    return str(arg1) + str(arg2)

MedAbrDirk/templates/MedAbrDirk/base.html

 {% load my_tags %}
                <div class="container-fluid">
                        <div class="row">
                                <div class="col-sm-3 col-md-2 sidebar">
                                        <ul class="nav nav-sidebar">
                                                <li class="dropdown">
                                                        <a href="#" class="dropdown-toggle-menu" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Rechnungen<span class="caret"></span></a>
                                                        <ul class="dropdown-menu">
                                                                {{'test'|kette:'test2'}}
                                                                <li><a href="{% url app_var|kette:'eingabe' %}">Rechnung eingeben<span class="sr-only">(current)</span></a></li>

Still, in the browser I get: "Invalid filter: 'kette'"

I have no idea what causes this. I have deleted the pychache folder, I have restarted my gunicorn several times.

Any advice?

halfer
  • 19,824
  • 17
  • 99
  • 186
MDoe
  • 163
  • 1
  • 13
  • And I also have an empty __init__.py in the templatetags folder. – MDoe Aug 29 '20 at 08:15
  • And I used manage.py shell to test the import. There on the shell I can import kette and if I call it with two arguments, it returns correctly their concatenation. – MDoe Aug 29 '20 at 08:17
  • is this [answer](https://stackoverflow.com/questions/6451304/django-simple-custom-template-tag-example) is your question? – satyajit Aug 29 '20 at 08:51
  • I don't see how. – MDoe Aug 29 '20 at 09:05

2 Answers2

0

docs To register custom filter with decorators you can do either of

# function name is taken as filter name
@register.filter
def kette(arg1, arg2):
    # code

OR

# value in name argument is taken as filter name
@register.filter(name='kette')
def kette(arg1, arg2):
    # code

You can either add name name argument in filter() or remove the parenthesis from filter()

You say you have a init.py file. In-order for python to consider it as a package __init__.py file should be there in templatetags folder. Not init.py.

So, In your app folder you should see a structure like this for custom template tags:

MedAbrDirk/ #app folder
MedAbrDirk/templatetags/
MedAbrDirk/templatetags/__init__.py # should have double underscore on both the sides
MedAbrDirk/templatetags/my_tags.py
Achuth Varghese
  • 2,356
  • 1
  • 4
  • 18
-1

Thanks for your help. It seems I figured it out by accident. In another app I also had a folder templatetags with a file my_tags.py. I thought these would be separately handled by Django. Obviously this is not the case.

So, after renaming the module, now it works.

halfer
  • 19,824
  • 17
  • 99
  • 186
MDoe
  • 163
  • 1
  • 13