I'm trying to do jwt authentication. I'm using some code from django-rest-auth package for registration and I have this problem for both registration endpoint and admin area. I know that a lot of people asked this question before me, but I am not sure why the solution isn't working in my case.
I've tried this in shell:
>>> from django.contrib.sites.models import Site
>>> site = Site()
>>> site.domain = 'answerly.com'
>>> site.name = 'answerly.com'
>>> site.save()
My settings:
...
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# local apps
'pages',
'accounts',
# third party packages
'rest_framework',
'rest_framework.authtoken',
'rest_auth',
'django.contrib.sites',
'allauth',
'allauth.account',
'rest_auth.registration',
]
...
REST_USE_JWT = True
...
Urls:
from django.contrib import admin
from django.urls import path, include
from rest_auth.registration.views import RegisterView
# from rest_auth.views import LoginView, LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.api.urls')),
path('accounts/registration/', RegisterView.as_view(), name='register'),
# path('rest-auth/registration/', include('rest_auth.registration.urls'))
]
I have this for the common solution:
Traceback (most recent call last):
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django
/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: UNIQUE constraint failed: django_site.domain
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/base.py", line 741, in save
force_update=force_update, update_fields=update_fields)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/base.py", line 779, in save_base
force_update, using, update_fields,
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/base.py", line 870, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/base.py", line 908, in _do_insert
using=using, raw=raw)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/query.py", line 1186, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1335, in execute_sql
cursor.execute(sql, params)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/valentyn/Dev/Answerly/venv/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: django_site.domain
I'm trying to better understand what is happening so please bear with me as I have a couple of questions:
Did you migrate "python manage.py migrate" after adding "'rest_framework.authtoken'," to your INSTALLED_APPS? refere to: https://django-rest-auth.readthedocs.io/en/latest/installation.html#
Did you add "SITE_ID = 1" in your settings.py? refer to: https://django-rest-auth.readthedocs.io/en/latest/installation.html#registration-optional