Home > Software design >  Unable to load the custom template tags in django
Unable to load the custom template tags in django

Time:12-01

templatetags : myapp_extras.py

from django import template

register = template.Library()

@register.simple_tag
def my_url(value,field_name,urlencode=None):
    url = '?{}={}'.format(field_name,value)
    if urlencode:
        querystring = urlencode.split('&')
        filtered_querystring = filter(lambda p:p.split('=')[0]!=field_name,querystring)
        encoded_querystring = '&'.join(filtered_querystring)
        url = '{}&{}'.format(url,encoded_querystring)

    return url

home.html 

{% load myapp_extras %}
.
.
.
<div >
    <span >
        {% if page_obj.has_previous %}
            <a href="{% my_url 1 'page' request.GET.urlencode%}">&laquo; first</a>
            <a href="{% my_url page_obj.previous_page_number 'page' request.GET.urlencode%}">previous</a>
        {% endif %}

        <span >
            Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
        </span>

        {% if page_obj.has_next %}
            <a href="{% my_url page_obj.next_page_number 'page' request.GET.urlencode%}">next</a>
            <a href="{% my_url page_obj.paginator.num_pages 'page' request.GET.urlencode%}">last &raquo;</a>
        {% endif %}
    </span>
</div>

settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'facligoapp'
]

views.py
def do_paginator(get_records_by_date,request):

    paginator = Paginator(get_records_by_date,10)
    page_number = request.GET.get('page', 1)
    try:
        page_obj  = paginator.get_page(page_number)
    except PageNotAnInteger:
        page_obj = paginator.page(1)
    except EmptyPage:
        page_obj = paginator.page(paginator.num_pages)
    return page_obj
:
:
        if new_records_check_box_status is None and error_records_check_box_status is None:
            get_records_by_date = Scrapper.objects.filter(start_time__date__range=(f_date, t_date))
            get_records_by_date = check_drop_down_status(get_records_by_date,drop_down_status)
            get_records_by_date = do_paginator(get_records_by_date,request)

Based on the my templates tags when I filter the datas the url should change. But the url is not changing and template tags is not working. I had created the init.py in template tags also. Is there any solution to change the structure of url as by templete tags does. When I change the next page the url is not changing.

CodePudding user response:

instead of this:

@register.simple_tag

try this:

@register.filter

And load it in template:

{% load filter_tags %}

Note: you must create empty init.py file inside templatetags directory.

After making above changes, you need to add this tag in settings.py file:

In settings.py file:

'libraries':{
                'filter_tags': 'templatetags.filter',
            }

If templatetags directory inside an app then you must add that appname in libraries.

 'libraries':{
                    'filter_tags': 'your_appanme.templatetags.filter',
                }

EX:

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_tags': 'templatetags.filter', #added here
            }
        },
    },
]

CodePudding user response:

if you create new templatetag file you need to restart the server to get it detected.

  • Related