Search code examples
pythondjangopython-3.xdjango-allauth

'filters' is not a registered tag library


I have an issue regarding template filter. I have used django allauth for user registration. I have edited its signup.html and used loop to iterate over the fields to show them dynamically. I could show the fields but could not define the type field.

What i did is

account/signup.html

{% load filters %}
<form class="signup" id="signup_form" method="post" action="{% url 'account_signup' %}">
    {% csrf_token %}
    {% for field in forms.visible_fields %}
        <input type="{{ field.field.widget|input_type}}" name="{{ field.name }}" id="{{ field.id_for_label}}" class="form-control">
    {% endfor %}
</form>

template filter inside main app(filters.py)

from django import template
register = template.Library()

@register.filter('input_type')
def input_type(ob):
    return ob.__class__.__name__

templates location

'DIRS': [os.path.join(BASE_DIR, 'templates')

As i am using django allauth where should i place my template filter code? Sign up form to extend the allauth form using first name and last name is inside main app.


Solution

  • The tag library should be placed in a templatetags directory in the root directory of the app:

    See code layout from the docs:

    When a Django app is added to INSTALLED_APPS, any tags it defines in the conventional location described below are automatically made available to load within templates.

    The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it - don’t forget the __init__.py file to ensure the directory is treated as a Python package.