Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'ugettext' from 'django.utils.translation'

I installed djangorestframework as shown below:

pip install djangorestframework -jwt

Then, I used rest_framework_jwt.views as shown below:

from rest_framework_jwt.views import (
    obtain_jwt_token, 
    refresh_jwt_token, 
    verify_jwt_token
)
...
    path('auth-jwt/', obtain_jwt_token),
    path('auth-jwt-refresh/',refresh_jwt_token),
    path('auth-jwt-verify/', verify_jwt_token),
...

But, I got the error below:

ImportError: cannot import name 'ugettext' from 'django.utils.translation'

So, how can I solve the error?

like image 656
KIRAN KUMAR N Avatar asked Sep 20 '25 12:09

KIRAN KUMAR N


1 Answers

from .serializers import ( File "*\lib\site-packages\rest_framework_jwt\serializers.py", line 7, in from django.utils.translation import ugettext as _
ImportError: cannot import name 'ugettext' from 'django.utils.translation' (c:\users\dell\downloads\djangoEnvs\djangoEnv\lib\site-packages\django\utils\translation_init_.py)

There are two ways of resolving above issues:

Way 1: (Not recommended Approach)
Steps:

  1. Open the last file in traceback (<path showing in your IDE>\lib\site-packages\rest_framework_jwt\serializers.py) :

  2. In this file, replace ugettext with --> gettext

  3. Enjoy coding!!!! :)

before replace:
from django.utils.translation import ugettext as _

after replace:
from django.utils.translation import gettext as _

Way2 : (Recommended Approach)

Steps are:

  1. Open settings.py (project folder)
  2. On the top of settings.py type below imports
    import django
    from django.utils.translation import gettext
    django.utils.translation.ugettext = gettext
    
  3. Enjoy Coding !!!!! :)
like image 152
chitreshpratapsingh Avatar answered Sep 23 '25 03:09

chitreshpratapsingh