menu

Questions & Answers

Could not import 'knox.auth.TokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'

I'm new to Django rest and I'm trying to create a very simple application that allows user to login/sign-up using knox. But as I try to run commands like "python manage.py makmigrations" or any other Django related commands, I get this error:

ImportError: Could not import 'knox.auth.TokenAuthentication' for API setting 'DEFAULT_AUTHENTICATION_CLASSES'. ImportError: cannot import name 'ugettext_lazy' from 'django.utils.trans lation' (C:\Users\user\Desktop\Proj\Server\env\lib\site-packages\django\utils\translation\__init__.py).

Here're parts of my settings.py file that I think are related to knox:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'rest_framework',
'knox',
'corsheaders',

'app',]


AUTH_USER_MODEL = 'app.User'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication', ),}
Answers(3) :

After spending hours trying to figure out why this is happening, I realized it's because I'm using Django 4.0!!!!!!

I downgraded to Django 3.2.11 and everything turned out to be ok:)

I could track a new Knox's version on project's github closed issue:

https://github.com/James1345/django-rest-knox/issues/260#issuecomment-1026060106

pypi link:

https://pypi.org/project/django-rest-knox/4.2.0/

hope it helps.

After searching on this problem because I have it too. I installed pip install django-rest-knox and it works for me.

Comments:
2023-01-24 00:30:05
Also make sure to use the latest version from django-rest-knox, because earlier versions didn't worked for me with the latest Django.