Djoser reset password email with Celery - django

I want to send reset password email using Celery. I try to override the reset_password method of the class djoser.views.UserViewSet:
class CustomUserViewSet(UserViewSet):
#action(["post"], detail=False)
def reset_password(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.get_user()
if user:
send_reset_password_email.delay(
self.request,
{'user': user},
[get_user_email(user)]
)
return Response(status=status.HTTP_200_OK)
But I get an error:
Traceback (most recent call last):
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 39, in _reraise_errors
yield
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 210, in dumps
payload = encoder(data)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\utils\json.py", line 68, in dumps
return _dumps(s, cls=cls or _default_encoder,
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\utils\json.py", line 58, in default
return super().default(o)
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Request is not JSON serializable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Dev\rich-peach\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Dev\rich-peach\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Dev\rich-peach\backend\api\views.py", line 51, in reset_password
send_reset_password_email.delay(
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\task.py", line 425, in delay
return self.apply_async(args, kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\task.py", line 575, in apply_async
return app.send_task(
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\base.py", line 788, in send_task
amqp.send_task_message(P, name, message, **options)
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\amqp.py", line 510, in send_task_message
ret = producer.publish(
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\messaging.py", line 166, in publish
body, content_type, content_encoding = self._prepare(
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\messaging.py", line 254, in _prepare
body) = dumps(body, serializer=serializer)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 209, in dumps
with _reraise_errors(EncodeError):
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 43, in _reraise_errors
reraise(wrapper, wrapper(exc), sys.exc_info()[2])
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\exceptions.py", line 21, in reraise
raise value.with_traceback(tb)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 39, in _reraise_errors
yield
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 210, in dumps
payload = encoder(data)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\utils\json.py", line 68, in dumps
return _dumps(s, cls=cls or _default_encoder,
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 238, in dumps
**kw).encode(obj)
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\utils\json.py", line 58, in default
return super().default(o)
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
kombu.exceptions.EncodeError: Object of type Request is not JSON serializable
Celery task:
#app.task(bind=True, default_retry_delay=5 * 60)
def send_reset_password_email(self, request, context, email):
try:
PasswordResetEmail(request, context).send(email)
except Exception as exc:
raise self.retry(exc=exc, countdown=60)
What I've already tried:
I tried to change the serializer for the task to 'pickle':
#app.task(bind=True, default_retry_delay=5 * 60, serializer='pickle')
But I get a new error:
Traceback (most recent call last):
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 39, in _reraise_errors
yield
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 210, in dumps
payload = encoder(data)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 334, in pickle_dumps
return dumper(obj, protocol=pickle_protocol)
TypeError: cannot pickle '_io.BufferedReader' object
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Dev\rich-peach\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Dev\rich-peach\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Dev\rich-peach\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Dev\rich-peach\backend\api\views.py", line 51, in reset_password
send_reset_password_email.delay(
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\task.py", line 425, in delay
return self.apply_async(args, kwargs)
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\task.py", line 575, in apply_async
return app.send_task(
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\base.py", line 788, in send_task
amqp.send_task_message(P, name, message, **options)
File "C:\Dev\rich-peach\venv\lib\site-packages\celery\app\amqp.py", line 510, in send_task_message
ret = producer.publish(
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\messaging.py", line 166, in publish
body, content_type, content_encoding = self._prepare(
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\messaging.py", line 254, in _prepare
body) = dumps(body, serializer=serializer)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 209, in dumps
with _reraise_errors(EncodeError):
File "C:\Users\lev_k\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 153, in __exit__
self.gen.throw(typ, value, traceback)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 43, in _reraise_errors
reraise(wrapper, wrapper(exc), sys.exc_info()[2])
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\exceptions.py", line 21, in reraise
raise value.with_traceback(tb)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 39, in _reraise_errors
yield
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 210, in dumps
payload = encoder(data)
File "C:\Dev\rich-peach\venv\lib\site-packages\kombu\serialization.py", line 334, in pickle_dumps
return dumper(obj, protocol=pickle_protocol)
kombu.exceptions.EncodeError: cannot pickle '_io.BufferedReader' object
How can I solve this problem?
Or how else can I do it?

Problem solved!
Instead of request, I pass the necessary fields to context, and instead of user - user.id:
#action(['post'], detail=False)
def reset_password(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.get_user()
if user:
send_reset_password_email.delay(
{
'user_id': user.id,
'domain': request.get_host(),
'protocol': 'https' if request.is_secure() else 'http',
'site_name': request.get_host()
},
[get_user_email(user)]
)
return Response(status=status.HTTP_200_OK)
This works because if you don't send a request to Password Reset E-mail, Djoser searches for the necessary fields in context:
class BaseEmailMessage(mail.EmailMultiAlternatives, ContextMixin):
def get_context_data(self, **kwargs):
ctx = super(BaseEmailMessage, self).get_context_data(**kwargs)
context = dict(ctx, **self.context)
if self.request:
site = get_current_site(self.request)
domain = context.get('domain') or (
getattr(settings, 'DOMAIN', '') or site.domain
)
protocol = context.get('protocol') or (
'https' if self.request.is_secure() else 'http'
)
site_name = context.get('site_name') or (
getattr(settings, 'SITE_NAME', '') or site.name
)
user = context.get('user') or self.request.user
else:
domain = context.get('domain') or getattr(settings, 'DOMAIN', '')
protocol = context.get('protocol') or 'http'
site_name = context.get('site_name') or getattr(
settings, 'SITE_NAME', ''
)
user = context.get('user')
context.update({
'domain': domain,
'protocol': protocol,
'site_name': site_name,
'user': user
})
return context
And in the task, I get the user by id, which I passed to context:
#app.task(bind=True, default_retry_delay=5 * 60)
def send_reset_password_email(self, context, email):
try:
context['user'] = CustomUser.objects.get(id=context.get('user_id'))
PasswordResetEmail(context=context).send(email)
except Exception as exc:
raise self.retry(exc=exc, countdown=60)

Related

requests.post(url, data)error `Response`, `HttpResponse` or `HttpStreamingResponse` but received a `<class 'tuple'>`

I am doing my first Django MongoDB project and trying to save the data set in the database
here is the create method that I'm using
def create(self, request, *args, **kwargs):
try:
client = MongoClient('srv13.absolute.ag:27017')
collection = client.farmos.device
header = request.headers['Authorization']
req = json.loads(request.body)
resp = requests.post(url=f"{settings.MAINFLUX_URL}/things", data=req, params=args, headers=header)
device_id = resp.headers['Location'][8:]
req['device_token'] = device_id
collection.insert_one(req)
return {'data': 'Success'}, 200
except Exception as e:
return {'data': str(e)}, 400
But it showing an error
and the error is
Internal Server Error: /api/v1/device/
Traceback (most recent call last):
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/adarsh/farmos/farmos/persistance_layer/mongodb/device_operations.py", line 53, in create
response = requests.post(url=f"{settings.MAINFLUX_URL}/things", data=json.dumps(req), headers=header)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/api.py", line 117, in post
return request('post', url, data=data, json=json, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/sessions.py", line 515, in request
prep = self.prepare_request(req)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/sessions.py", line 453, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/models.py", line 319, in prepare
self.prepare_headers(headers)
File "/home/adarsh/farmos/venv/lib/python3.6/site-packages/requests/models.py", line 451, in prepare_headers
for header in headers.items():
AttributeError: 'str' object has no attribute 'items'
I think there is a problem in this line
python
resp = requests.post(url=f"{settings.MAINFLUX_URL}/things", data=req, params=args, headers=header)
I have tried different thing changing the type of req and header to solve it but couldn't do it

django-rest-auth with allauth customuser TypeError: 'PhoneNumber' object is not subscriptable

models.py
class CustomUser(AbstractUser):
username = PhoneNumberField(unique=True)
payloads:
{
"username": "+8801700000000",
"password1": "demo",
"password2": "demo",
"email": "demo#demo.com",
}
response:
Internal Server Error: /api/rest-auth/registration/
Traceback (most recent call last):
File "venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "venv\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "venv\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "venv\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "venv\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "venv\lib\site-packages\rest_auth\registration\views.py", line 46, in dispatch
return super(RegisterView, self).dispatch(*args, **kwargs)
File "venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "venv\lib\site-packages\rest_framework\generics.py", line 190, in post
return self.create(request, *args, **kwargs)
File "venv\lib\site-packages\rest_auth\registration\views.py", line 65, in create
user = self.perform_create(serializer)
File "venv\lib\site-packages\rest_auth\registration\views.py", line 73, in perform_create
user = serializer.save(self.request)
File "venv\lib\site-packages\rest_auth\registration\serializers.py", line 210, in save
adapter.save_user(request, user, self)
File "venv\lib\site-packages\allauth\account\adapter.py", line 242, in save_user
self.populate_username(request, user)
File "venv\lib\site-packages\allauth\account\adapter.py", line 209, in populate_username
user_username(
File "venv\lib\site-packages\allauth\account\utils.py", line 120, in user_username
return user_field(user, app_settings.USER_MODEL_USERNAME_FIELD, *args)
File "venv\lib\site-packages\allauth\account\utils.py", line 110, in user_field
v = v[0:max_length]
TypeError: 'PhoneNumber' object is not subscriptable
i think this problem because all auth try to subscribe the username by using slicing i recommend to try the following
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True) # validators should be a list
this token from
What's the best way to store Phone number in Django models

Unable to use custom account adapter

I have the following in adapter.py:
from allauth.account.adapter import DefaultAccountAdapter
class CustomAllauthAdapter(DefaultAccountAdapter):
pass # keeping it trivial for debugging
At the very bottom of settings.py:
import django
django.setup() # complains about apps not being loaded yet without this...
from .adapter import CustomAllauthAdapter
ACCOUNT_ADAPTER = CustomAllauthAdapter # this is the line that results in the error!
As soon as I submit the registration form for a new user, I get this error in the browser:
AssertionError at /api/v1/users/auth/register/
No exception message supplied
Here is the Traceback:
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\django\views\decorators\debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\dj_rest_auth\registration\views.py", line 47, in dispatch
return super().dispatch(*args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\generics.py", line 190, in post
return self.create(request, *args, **kwargs)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\dj_rest_auth\registration\views.py", line 66, in create
serializer.is_valid(raise_exception=True)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\serializers.py", line 419, in run_validation
value = self.to_internal_value(data)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\rest_framework\serializers.py", line 478, in to_internal_value
validated_value = validate_method(validated_value)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\dj_rest_auth\registration\serializers.py", line 209, in validate_email
email = get_adapter().clean_email(email)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\allauth\account\adapter.py", line 535, in get_adapter
return import_attribute(app_settings.ADAPTER)(request)
File "C:\Dropbox\Parnasa\Web\drmeir\env\lib\site-packages\allauth\utils.py", line 153, in import_attribute
assert isinstance(path, str)
If I do not set the ACCOUNT_ADAPTER, everything works well including registration and authentication. I need to use custom account adapter to customize email verification.
Why am I getting the error and what should I do to fix it?
No need to import anything and no need for django.setup(). The configuration variable is a string!
ACCOUNT_ADAPTER = 'users.adapter.CustomAllauthAdapter'

django.urls.exceptions.NoReverseMatch: Reverse for 'account_inactive' not found. 'account_inactive' is not a valid view function or pattern name

I am using django-rest-auth registration api, I overridden that by using my own SERIALIZER and model. So I am getting error.
I have tried by setting active at the time user created. But that is not working.
Let me know how to resolve this type of problem.
[12/Jan/2019 17:24:06] "POST /rest-auth/registration/ HTTP/1.1" 400 68
Internal Server Error: /rest-auth/registration/
Traceback (most recent call last):
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_auth/registration/views.py", line 46, in dispatch
return super(RegisterView, self).dispatch(*args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/generics.py", line 192, in post
return self.create(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_auth/registration/views.py", line 65, in create
user = self.perform_create(serializer)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_auth/registration/views.py", line 81, in perform_create
None)
File "/Users/test/project/vblah/lib/python3.6/site-packages/allauth/account/utils.py", line 183, in complete_signup
signal_kwargs=signal_kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/allauth/account/utils.py", line 133, in perform_login
return adapter.respond_user_inactive(request, user)
File "/Users/test/project/vblah/lib/python3.6/site-packages/allauth/account/adapter.py", line 454, in respond_user_inactive
reverse('account_inactive'))
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'account_inactive' not found. 'account_inactive' is not a valid view function or pattern name.```
this exception happend because in your User model
is_active = False .
so set True and migrate
in models.py set is_active = True
and migrate the tables

IndexError: list index out of range in Test case

I have strange problems
I have next code Using TesCase and APIClient to get request with token
class Profile(TestCase):
fixtures = ['profiles.json']
def setUp(self):
self.client = APIClient()
self.user = Profile.objects.get(username='some-username')
self.token = Token.objects.create(user=self.user)
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key)
def test_profile(self):
response = self.client.get('/api/some-username/profile/')
print(response.status_code)
print(response.data)`
Authorization goes well,but the I received error. If i use requests.get with headers (token) to development django server all pass well i receive a correct json response
Full Traceback when i run this test:
Traceback (most recent call last):
File "/home/work/project/profiles/tests.py", line 103, in test_user_profile
response = self.client.get('/api/some-username/profile/')
File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 291, in get
response = super(APIClient, self).get(path, data=data, **extra)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 208, in get
return self.generic('GET', path, **r)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 237, in generic
method, path, data, content_type, secure, **extra)
File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 416, in generic
return self.request(**r)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 288, in request
return super(APIClient, self).request(**kwargs)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/test.py", line 240, in request
request = super(APIRequestFactory, self).request(**kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/test/client.py", line 501, in request
six.reraise(*exc_info)
File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 686, in reraise
raise value
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python3.5/dist-packages/rest_framework_tracking/mixins.py", line 69, in handle_exception
response = super(BaseLoggingMixin, self).handle_exception(exc)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/generics.py", line 254, in get
return self.retrieve(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/mixins.py", line 58, in retrieve
return Response(serializer.data)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 533, in data
ret = super(Serializer, self).data
File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 262, in data
self._data = self.to_representation(self.instance)
File "/usr/local/lib/python3.5/dist-packages/rest_framework/serializers.py", line 500, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "/home/work/project/profiles/serializers.py", line 10, in to_representation
return institute.all()[0].slug
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 289, in __getitem__
return list(qs)[0]
IndexError: list index out of range
How know what is the problem?