'I have an error about smart_text after installing django-admin-charts

I want to have charts in the admin panel, I installed the django-admin-charts package, Which said add these to settings.py

INSTALLED_APPS = (
         'admin_tools_stats', # this must be BEFORE 'admin_tools' and 'django.contrib.admin'
         'django_nvd3',
)

But after adding it, it gives this error:

ImportError: cannot import name 'smart_text' from 'django.utils.encoding' (E: \ test1 \ venv1 \ lib \ site-packages \ django \ utils \ encoding.py)

I searched but didn't find much about smart_text! Maybe it has a problem with Django version 4 or Python version 3.10.3?



Solution 1:[1]

I digged around a little and the problem seems to be the Django version. smart_text function is not present in the django/utils/encoding.py file. I downgraded from 4.0.2 to 3.2.13, set PyMemcacheCache as the default cache in settings.py and it works. In my case downgrading is not acceptable as Django. 3.2.13 doesn't support redis cache, but it might help you. Let's hope that django-admin-charts will add suport for Django 4.0.

Solution 2:[2]

this is beacuse django-admin-charts needs smart_text and in django 4 smart_text is removed from the utils/encoding so the better way to resolve this issue is to add this code in your settings.py file

import django from django.utils.encoding import smart_str django.utils.encoding.smart_text = smart_str

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Trist4n
Solution 2 Yemi Bold