[Fixed]-How to access the next and the previous elements in a Django template forloop?

20๐Ÿ‘

โœ…

You can create custom template filters next and previous which returns the next and the previous elements of the for loop respectively.

from django import template

register = template.Library()

@register.filter
def next(some_list, current_index):
    """
    Returns the next element of the list using the current index if it exists.
    Otherwise returns an empty string.
    """
    try:
        return some_list[int(current_index) + 1] # access the next element
    except:
        return '' # return empty string in case of exception

@register.filter
def previous(some_list, current_index):
    """
    Returns the previous element of the list using the current index if it exists.
    Otherwise returns an empty string.
    """
    try:
        return some_list[int(current_index) - 1] # access the previous element
    except:
        return '' # return empty string in case of exception

Then in your template you can do the following to access the next and previous elements.

{% with next_element=some_list|next:forloop.counter0 %} # assign next element to a variable
{% with previous_element=some_list|previous:forloop.counter0 %} # assign previous element to a variable

Final Code:

{% for element in list %}         
    {% with next_element=list|next:forloop.counter0 %} # get the next element 
    {% with previous_element=list|previous:forloop.counter0 %} # get the previous element 

        {% if element.is_child and not previous_element.is_child %}
            <div class="children-block">
        {% endif %}
            {{ element.title }}
        {% if element.is_child and not next_element.is_child %}
            </div>
        {% endif %}

    {% endwith %}
    {% endwith %}
{% endfor %}
๐Ÿ‘คRahul Gupta

1๐Ÿ‘

I modified the code from the accepted answer a little bit to get it though my linters.

from typing import Any

@register.filter(name="next")
def next(some_list: list[Any], current_index: int) -> Any:
    """Returns the next element of the list using the current index if it exists. Otherwise returns an empty string."""
    if not some_list or current_index == len(some_list) - 1:
        return ""
    return some_list[current_index + 1]  # access the next element

@register.filter(name="previous")
def previous(some_list: list[Any], current_index: int) -> Any:
    """Returns the previous element of the list using the current index if it exists. Otherwise returns an empty string."""
    if not some_list or current_index == 0:
        return ""
    return some_list[current_index - 1]  # access the previous element
๐Ÿ‘คFreiholtz

Leave a comment