EMAIL BACKEND does not work give me error : ( No connection could be made because the target machine actively refused it)

this is my views.py :

from django.contrib.auth import get_user_model
from dj_rest_auth.registration.views import RegisterView
from dj_rest_auth.views import UserDetailsView
from users.serializers import CustomRegisterSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from allauth.account.models import EmailConfirmation, EmailConfirmationHMAC
from .serializers import CustomUserDetailsSerializer
from rest_framework import status
from django.utils import timezone


class CustomRegisterView(RegisterView):
    serializer_class = CustomRegisterSerializer

    def post(self, request, *args, **kwargs):
        if request.user.is_authenticated:
            return Response(
                {"detail": "You are already registered and logged in."},
                status=status.HTTP_400_BAD_REQUEST
            )
        return super().post(request, *args, **kwargs)

    def perform_create(self, serializer):
        user = serializer.save(self.request)
        email_address = user.emailaddress_set.get(email=user.email)
        email_confirmation = EmailConfirmation.create(email_address)
        email_confirmation.sent = timezone.now()
        email_confirmation.save()


class CustomUserDetailsView(UserDetailsView):
    serializer_class = CustomUserDetailsSerializer


class EmailConfirmationView(APIView):
    def get(self, request, key):
        try:
            email_confirmation = self.get_email_confirmation(key)
        except EmailConfirmation.DoesNotExist:
            return Response({'error': 'Invalid key'}, status=status.HTTP_400_BAD_REQUEST)

        if email_confirmation.email_address.verified:
            return Response({'message': 'Email already verified'}, status=status.HTTP_200_OK)

        email_confirmation.confirm()
        user = self.get_user(email_confirmation)

        # Perform additional actions if needed, such as updating user fields
        user.is_active = True
        user.save()

        return Response({'message': 'Email confirmed successfully'}, status=status.HTTP_200_OK)

    def get_email_confirmation(self, key):
        email_confirmation = EmailConfirmationHMAC.from_key(key)
        if not email_confirmation:
            raise EmailConfirmation.DoesNotExist
        return email_confirmation

    def get_user(self, email_confirmation):
        User = get_user_model()
        return User.objects.get(email=email_confirmation.email_address.email)

this is my urls.py :

from users.views import CustomUserDetailsView
from .views import (
    ProjectViewSets,
    ThumbnailViewSets,
    ProjectByCategoryAPI,
)
from dj_rest_auth.registration.views import VerifyEmailView
from rest_framework.routers import DefaultRouter
from django.urls import path, include, re_path
from dj_rest_auth.views import (
    PasswordChangeView,
    PasswordResetView,
    PasswordResetConfirmView,
)
from users.views import (
    CustomRegisterView,
    CustomUserDetailsView,
    EmailConfirmationView
)


router = DefaultRouter()
router.register("projects", ProjectViewSets, basename="project")
router.register("images", ThumbnailViewSets, basename="image")


urlpatterns = [
    path("password/reset/", PasswordResetView.as_view(),
         name="rest_password_reset"),
    path(
        "password/reset/confirm/<uidb64>/<token>/",
        PasswordResetConfirmView.as_view(),
        name="password_reset_confirm",
    ),
    path("", include(router.urls)),
    path(
        "projects/<str:category>/filter",
        ProjectByCategoryAPI.as_view(),
        name="project_list",
    ),
    path("dj-rest-auth/", include("dj_rest_auth.urls")),
    path(
        "dj-rest-auth/registration/", CustomRegisterView.as_view(), name="rest_register"
    ),
    re_path(
        "account-confirm-email/",
        VerifyEmailView.as_view(),
        name="account_email_verification_sent",
    ),
    re_path(
        "account-confirm-email/(?P<key>[-:\w]+)/$",
        VerifyEmailView.as_view(),
        name="account_confirm_email",
    ),
    path('email-confirmation/<str:key>/', EmailConfirmationView.as_view()),
    path(
        "dj-rest-auth/user/", CustomUserDetailsView().as_view(), name="user-details"
    ),
]

Side note: When posting code here, please remember to surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your previous post to include this.)

Where are you actually creating and sending the email?

I had suggested earlier that you write a test script separate from your Django project to try sending an email. Did you do that? If so, that’s the code I’d like to see, along with the output from it.

1 Like