django-jazzmin icon indicating copy to clipboard operation
django-jazzmin copied to clipboard

Invalid filter: 'length_is' in change_form with Django 5.1

Open guercusguo opened this issue 1 year ago • 10 comments

Due to: The length_is template filter is removed in Features removed in 5.1.

In template /env/lib/python3.12/site-packages/jazzmin/templates/admin/includes/fieldset.html, error at line 22

Invalid filter: 'length_is'

12
{{ fieldset.description }}
{%endif%}  
{% endif %}   {% for line in fieldset %}
{% for field in line %}

guercusguo avatar Aug 12 '24 09:08 guercusguo

Also seeing this - is there a known fix?

collinsinclair avatar Aug 14 '24 00:08 collinsinclair

This is solution, change your django version, downgrade to 5.0.1

pip3 install django==5.0.1

krv006 avatar Aug 14 '24 05:08 krv006

As a temporary workaround, copy over the file templates/admin/includes/fieldset.html from jazzmin to your project and replace the length_is filter as explained in the Django release notes: https://docs.djangoproject.com/en/5.1/releases/4.2/#id1

My working fieldset.html for Django 5.1 looks like this now:

{% load jazzmin %}
{% if card %}
<div class="card {{ fieldset.classes|cut:"collapse" }}">
    {% if card_header and fieldset.name %}
        <div class="card-header">
            <div class="card-title">
                <strong>{{ fieldset.name }}</strong>{% if fieldset.description %} - <i>{{ fieldset.description }}</i>{% endif %}
            </div>
        </div>
    {%elif fieldset.description %}
    <div class="card-header">
        <div class="card-title">
            {{ fieldset.description }}
        </div>
    </div>
    {%endif%}

    <div class="p-5{% if fieldset.name %} card-body{% endif %}">
{% endif %}

    {% for line in fieldset %}
    <div class="form-group{% if line.fields|length == 1 and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
        <div class="row">
            {% for field in line %}
                <label class="{% if not line.fields|length == 1 and forloop.counter != 1 %}col-auto {% else %}col-sm-3 {% endif %}text-left" for="id_{{ field.field.name }}">
                    {{ field.field.label|capfirst }}
                    {% if field.field.field.required %}
                    <span class="text-red">* </span>
                    {% endif %}
                </label>
                <div class="{% if not line.fields|length == 1 %} col-auto  fieldBox {% else %} col-sm-7 {% endif %}
                             {% if field.field.name %} field-{{ field.field.name }}{% endif %}
                             {% if not field.is_readonly and field.errors %} errors{% endif %}
                             {% if field.field.is_hidden %} hidden {% endif %}
                             {% if field.is_checkcard %} checkcard-row{% endif %}">
                    {% if field.is_readonly %}
                        <div class="readonly">{{ field.contents }}</div>
                    {% else %}
                        {{ field.field }}
                    {% endif %}
                    <div class="help-block red">
                        {% if not line.fields|length == 1 and not field.is_readonly %}{{ field.errors }}{% endif %}
                    </div>
                    {% if field.field.help_text %}
                        <div class="help-block">{{ field.field.help_text|safe }}</div>
                    {% endif %}
                    <div class="help-block text-red">
                        {% if line.fields|length == 1 %}{{ line.errors }}{% endif %}
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
    {% endfor %}

{% if card %}
    </div>
</div>
{% endif %}

ronny-rentner avatar Aug 14 '24 08:08 ronny-rentner

you must be install Django==5.0.1 because Django update 7 days ago and you can read https://docs.djangoproject.com/en/5.1/releases/5.1/ this web sayt

ср, 14 авг. 2024 г. в 13:01, Ronny Rentner @.***>:

As a temporary workaround, copy over the file templates/admin/includes/fieldset.html from jazzmin to your project and replace the length_is filter as explained in the Django release notes: https://docs.djangoproject.com/en/5.1/releases/4.2/#id1

My working fieldset.html for Django 5.1 looks like this now:

{% load jazzmin %} {% if card %}

{% if card_header and fieldset.name %}
{{ fieldset.name }}{% if fieldset.description %} - {{ fieldset.description }}{% endif %}
{%elif fieldset.description %}
{{ fieldset.description }}
{%endif%}
<div class="p-5{% if fieldset.name %} card-body{% endif %}">

{% endif %}

{% for line in fieldset %}
<div class="form-group{% if line.fields|length == 1 and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
    <div class="row">
        {% for field in line %}
            <label class="{% if not line.fields|length == 1 and forloop.counter != 1 %}col-auto {% else %}col-sm-3 {% endif %}text-left" for="id_{{ field.field.name }}">
                {{ field.field.label|capfirst }}
                {% if field.field.field.required %}
                <span class="text-red">* </span>
                {% endif %}
            </label>
            <div class="{% if not line.fields|length == 1 %} col-auto  fieldBox {% else %} col-sm-7 {% endif %}
                         {% if field.field.name %} field-{{ field.field.name }}{% endif %}
                         {% if not field.is_readonly and field.errors %} errors{% endif %}
                         {% if field.field.is_hidden %} hidden {% endif %}
                         {% if field.is_checkcard %} checkcard-row{% endif %}">
                {% if field.is_readonly %}
                    <div class="readonly">{{ field.contents }}</div>
                {% else %}
                    {{ field.field }}
                {% endif %}
                <div class="help-block red">
                    {% if not line.fields|length == 1 and not field.is_readonly %}{{ field.errors }}{% endif %}
                </div>
                {% if field.field.help_text %}
                    <div class="help-block">{{ field.field.help_text|safe }}</div>
                {% endif %}
                <div class="help-block text-red">
                    {% if line.fields|length == 1 %}{{ line.errors }}{% endif %}
                </div>
            </div>
        {% endfor %}
    </div>
</div>
{% endfor %}

{% if card %}

{% endif %}

— Reply to this email directly, view it on GitHub https://github.com/farridav/django-jazzmin/issues/593#issuecomment-2288096357, or unsubscribe https://github.com/notifications/unsubscribe-auth/BFE2FFDN5SMOABI3XXLLFWTZRMFETAVCNFSM6AAAAABML2GXW2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOBYGA4TMMZVG4 . You are receiving this because you commented.Message ID: @.***>

krv006 avatar Aug 14 '24 11:08 krv006

Hope that #519 will be merged

JargeZ avatar Aug 20 '24 02:08 JargeZ

As a temporary workaround, copy over the file templates/admin/includes/fieldset.html from jazzmin to your project and replace the length_is filter as explained in the Django release notes: https://docs.djangoproject.com/en/5.1/releases/4.2/#id1

My working fieldset.html for Django 5.1 looks like this now:

{% load jazzmin %}
{% if card %}
<div class="card {{ fieldset.classes|cut:"collapse" }}">
    {% if card_header and fieldset.name %}
        <div class="card-header">
            <div class="card-title">
                <strong>{{ fieldset.name }}</strong>{% if fieldset.description %} - <i>{{ fieldset.description }}</i>{% endif %}
            </div>
        </div>
    {%elif fieldset.description %}
    <div class="card-header">
        <div class="card-title">
            {{ fieldset.description }}
        </div>
    </div>
    {%endif%}

    <div class="p-5{% if fieldset.name %} card-body{% endif %}">
{% endif %}

    {% for line in fieldset %}
    <div class="form-group{% if line.fields|length == 1 and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
        <div class="row">
            {% for field in line %}
                <label class="{% if not line.fields|length == 1 and forloop.counter != 1 %}col-auto {% else %}col-sm-3 {% endif %}text-left" for="id_{{ field.field.name }}">
                    {{ field.field.label|capfirst }}
                    {% if field.field.field.required %}
                    <span class="text-red">* </span>
                    {% endif %}
                </label>
                <div class="{% if not line.fields|length == 1 %} col-auto  fieldBox {% else %} col-sm-7 {% endif %}
                             {% if field.field.name %} field-{{ field.field.name }}{% endif %}
                             {% if not field.is_readonly and field.errors %} errors{% endif %}
                             {% if field.field.is_hidden %} hidden {% endif %}
                             {% if field.is_checkcard %} checkcard-row{% endif %}">
                    {% if field.is_readonly %}
                        <div class="readonly">{{ field.contents }}</div>
                    {% else %}
                        {{ field.field }}
                    {% endif %}
                    <div class="help-block red">
                        {% if not line.fields|length == 1 and not field.is_readonly %}{{ field.errors }}{% endif %}
                    </div>
                    {% if field.field.help_text %}
                        <div class="help-block">{{ field.field.help_text|safe }}</div>
                    {% endif %}
                    <div class="help-block text-red">
                        {% if line.fields|length == 1 %}{{ line.errors }}{% endif %}
                    </div>
                </div>
            {% endfor %}
        </div>
    </div>
    {% endfor %}

{% if card %}
    </div>
</div>
{% endif %}

Tnxs, i was wondering if I didn't do something right, because I used it 3 weeks ago with Django 5.0 it worked, I was like why

daniel-devi avatar Sep 02 '24 16:09 daniel-devi

To clarify the above comment, you should have a folder specified in your settings.py file for 'templates':

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            'templates',
            ],
        'APP_DIRS': True,
        ...
   }
]

Then copy the file from the previous comment into the 'templates' using the same path as the file in the jazzmin package, like templates\admin\includes\fieldset.html.

Then copy/edit the file so that it matches the above.

AndrewManHayChiu avatar Sep 07 '24 11:09 AndrewManHayChiu

Hi, is this fixed there currently SQL Injection(not only) issue with Django==5.0.1 version which is the workaround of this issue. @farridav

mikebgrep avatar Oct 12 '24 11:10 mikebgrep

This has been fixed with https://github.com/farridav/django-jazzmin/releases/tag/v3.0.1

FabianClemenz avatar Oct 21 '24 07:10 FabianClemenz

Okay, thanks. I will update to this version.

mikebgrep avatar Oct 21 '24 08:10 mikebgrep

This is solution, change your django version, downgrade to 5.0.1

pip3 install django==5.0.1

Since length_is is deprecated since Django 4.2 and removed in Django 5.1

See changes : https://github.com/farridav/django-jazzmin/pull/519/commits/15b4ed076a2db0e65b6ad32bd9d459f4a85ccceb

You can use pip install Django==4.1.* to fix issue.

imvickykumar999 avatar Sep 02 '25 12:09 imvickykumar999