Ashok Kumar
Ashok Kumar

Reputation: 334

ConnectionRefusedError in dJango rest api while registration process

ConnectionRefusedError error showing when register user,

basic information added on database but password field was blank and other database fields submitted please find the following error and our class code,

Class

class ProfessionalRegistrationSerializer(serializers.HyperlinkedModelSerializer):

password = serializers.CharField(max_length=20, write_only=True)


email = serializers.EmailField()
first_name = serializers.CharField(max_length=30)
last_name = serializers.CharField(max_length=30)

class Meta:
    model = User

    fields = ('url', 'id', 'first_name', 'last_name', 'email', 'password')

def validate_email(self, value):
    from validate_email_address import validate_email
    if User.all_objects.filter(email=value.lower()).exists():
        raise serializers.ValidationError('User with this email already exists.')

    return value.lower()

def create(self, validated_data):
    password = validated_data.pop('password')
    email = validated_data.pop('email')
    user = User.objects.create(
        username=email.lower(),
        email=email.lower(),
        role_id=1,
        **validated_data)
    user.set_password(password)

    user.save()

    return user

Error

ConnectionRefusedError at /api/v1/register/professional/ [Errno 111] Connection refused Request Method: POST Request URL: http://127.0.0.1:8000/api/v1/register/professional/ Django Version: 1.8.14 Exception Type: ConnectionRefusedError Exception Value:
[Errno 111] Connection refused Exception Location: /usr/lib/python3.5/socket.py in create_connection, line 702 Python Executable: /home/project_backend/env/bin/python Python Version: 3.5.2 Python Path:
['/home/project_backend', '/home/project_backend/env/lib/python35.zip', '/home/project_backend/env/lib/python3.5', '/home/project_backend/env/lib/python3.5/plat-x86_64-linux-gnu', '/home/project_backend/env/lib/python3.5/lib-dynload', '/usr/lib/python3.5', '/usr/lib/python3.5/plat-x86_64-linux-gnu', '/home/project_backend/env/lib/python3.5/site-packages', '/home/project_backend/env/lib/python3.5/site-packages/setuptools-36.0.1-py3.5.egg']

Traceback

File "/home/project_backend/env/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response

132.response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/project_backend/env/lib/python3.5/site-packages/django/views/decorators/csrf.py" in wrapped_view 58. return view_func(*args, **kwargs) File "/home/project_backend/env/lib/python3.5/site-packages/django/views/generic/base.py" in view 71. return self.dispatch(request, *args, **kwargs) File "/home/project_backend/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch 464. response = self.handle_exception(exc) File "/home/project_backend/env/lib/python3.5/site-packages/rest_framework/views.py" in dispatch 461. response = handler(request, *args, **kwargs) File "/home/project_backend/filmup/apps/registrations/views.py" in post 53. user = serializer.save(work_status=user_type) File "/home/project_backend/env/lib/python3.5/site-packages/rest_framework/serializers.py" in save 175. self.instance = self.create(validated_data) File "/home/project_backend/project/apps/registrations/serializers.py" in create 157. **validated_data) File "/home/project_backend/env/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method 127. return getattr(self.get_queryset(), name)(*args, **kwargs) File "/home/project_backend/env/lib/python3.5/site-packages/django/db/models/query.py" in create 348. obj.save(force_insert=True, using=self.db) File "/home/project_backend/project/libs/accounts/models.py" in save 217. super().save(*args, **kwargs) File "/home/project_backend/env/lib/python3.5/site-packages/django/db/models/base.py" in save 734. force_update=force_update, update_fields=update_fields) File "/home/project_backend/env/lib/python3.5/site-packages/django/db/models/base.py" in save_base 771. update_fields=update_fields, raw=raw, using=using) File "/home/project_backend/env/lib/python3.5/site-packages/django/dispatch/dispatcher.py" in send 189. response = receiver(signal=self, sender=sender, **named) File "/home/project_backend/filmup/libs/accounts/signals.py" in create_user_setting 19. create_ejabberd_user(instance) File "/home/project_backend/project/libs/accounts/signals.py" in create_ejabberd_user 11. EjabberdUser.objects.create(username=str(user.id), password=str(token.key)) File "/home/project_backend/project/libs/accounts/models.py" in create 73. ctl.register(user=kwargs['username'], password=kwargs['password']) File "/home/project_backend/project/libs/ejabberdctl.py" in register 54. 'password': password}) File "/home/project_backend/project/libs/ejabberdctl.py" in ctl 32. return fn(self.params, payload) File "/usr/lib/python3.5/xmlrpc/client.py" in call 1092. return self.__send(self.__name, args) File "/usr/lib/python3.5/xmlrpc/client.py" in __request 1432. verbose=self.__verbose File "/usr/lib/python3.5/xmlrpc/client.py" in request 1134. return self.single_request(host, handler, request_body, verbose) File "/usr/lib/python3.5/xmlrpc/client.py" in single_request 1146. http_conn = self.send_request(host, handler, request_body, verbose) File "/usr/lib/python3.5/xmlrpc/client.py" in send_request 1259. self.send_content(connection, request_body) File "/usr/lib/python3.5/xmlrpc/client.py" in send_content 1289. connection.endheaders(request_body) File "/usr/lib/python3.5/http/client.py" in endheaders 1102. self._send_output(message_body) File "/usr/lib/python3.5/http/client.py" in _send_output 934. self.send(msg) File "/usr/lib/python3.5/http/client.py" in send 877. self.connect() File "/usr/lib/python3.5/http/client.py" in connect 849. (self.host,self.port), self.timeout, self.source_address) File "/usr/lib/python3.5/socket.py" in create_connection 711. raise err File "/usr/lib/python3.5/socket.py" in create_connection 702. sock.connect(sa)

Upvotes: 3

Views: 3518

Answers (3)

Mahboob A
Mahboob A

Reputation: 11

if you have used email as username field, (i.e. in custom User model class: USERNAME_FIELD = 'email') and using dj_rest_auth and allauth for auth,

then follow along.

if you are testing auth and does not just want email to be sent now, then add the below in settings.py -

non email verification

ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_VERIFICATION = "none"

for email verification:

ACCOUNT_AUTHENTICATION_METHOD = "email"
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 1
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_USERNAME_REQUIRED = False

if you are using username as username-filed, then add the below in settings.py as iamsaiful has noted.

ACCOUNT_EMAIL_VERIFICATION = 'none'
ACCOUNT_AUTHENTICATION_METHOD = 'username'
ACCOUNT_EMAIL_REQUIRED = False

Upvotes: 1

imsaiful
imsaiful

Reputation: 1604

I was getting the same error and it may be due to email verification. I add following code in my setting.py file and now authentication working perfectly


ACCOUNT_EMAIL_VERIFICATION = 'none'

ACCOUNT_AUTHENTICATION_METHOD = 'username'

ACCOUNT_EMAIL_REQUIRED = False

Upvotes: 9

Linovia
Linovia

Reputation: 21006

You perform a call to a remote server that you can't reach / isn't configured / isn't running.

It's not an issue with Django or DRF.

Upvotes: 1

Related Questions