1

我正在尝试使 Django 组织的自定义邀请工作。但它抛出了一个错误。我找不到原因。下面是我的代码:

我的urls.py档案

from django.urls import path, include
from organizations.backends import invitation_backend

urlpatterns = [
    path('accounts/', include('organizations.urls')),
    path('invitations/', include(invitation_backend().get_urls())),
]

在我的settings.py文件中,我在已安装的应用程序部分添加了“组织”应用程序和我的自定义应用程序“集成”。需要在以下链接中为自定义邀请添加后端:https ://django-organizations.readthedocs.io/en/latest/custom_usage.html#creating-the-backend

这是指定的后端。

INVITATION_BACKEND = 'integration.backends.CustomInvitations'

我的backends.py档案

from organizations.backends.defaults import InvitationBackend


class CustomInvitations(InvitationBackend):
    def invite_by_email(self, email, sender=None, request=None, **kwargs):
      try:
          user = self.user_model.objects.get(email=email)
      except self.user_model.DoesNotExist:
          user = self.user_model.objects.create(email=email,
                  password=self.user_model.objects.make_random_password())
          user.is_active = False
          user.save()
      self.send_invitation(user, sender, **kwargs)
      return user

这是发生的错误:

Traceback (most recent call last):
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\mixins.py", line 111, in dispatch
    return super(AdminRequiredMixin, self).dispatch(request, *args,
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
    return handler(request, *args, **kwargs)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\views.py", line 135, in post
    return super(BaseOrganizationUserCreate, self).post(request, *args, **kwargs)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\edit.py", line 142, in post
    return self.form_valid(form)
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\edit.py", line 125, in form_valid
    self.object = form.save()
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\forms.py", line 111, in save
    invitation_backend().send_notification(user, **{
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\backends\defaults.py", line 317, in send_notification
    self.email_message(user, self.notification_subject, self.notification_body, sender, **kwargs).send()
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\mail\message.py", line 276, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "c:\users\acer\appdata\local\programs\python\python38\lib\smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "c:\users\acer\appdata\local\programs\python\python38\lib\smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "c:\users\acer\appdata\local\programs\python\python38\lib\smtplib.py", line 308, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "c:\users\acer\appdata\local\programs\python\python38\lib\socket.py", line 808, in create_connection
    raise err
  File "c:\users\acer\appdata\local\programs\python\python38\lib\socket.py", line 796, in create_connection
    sock.connect(sa)

Exception Type: ConnectionRefusedError at /accounts/1/people/add/
Exception Value: [WinError 10061] No connection could be made because the target machine actively refused it
4

1 回答 1

0

几分钟前,我自己刚刚遇到了这个错误。该错误告诉您它正在尝试使用配置的 SMTP 服务器发送电子邮件,但机器拒绝连接。短版 - 您需要指向一个正在运行的 SMTP 服务器。

我无法找到指向远程 SMTP 服务器的正确设置,但我能够做的是运行本地假 SMTP 服务器以消除错误并至少让该过程运行完成。

我通过安装和运行nullsmtpd做到了这一点。

使用以下命令安装包:

pip install nullsmtpd

创建一个文件夹来存储电子邮件和日志:

mkdir email

在 Windows 上启动该过程并将其指向您的输出文件夹:

nullsmtpd --mail-dir email --no-fork

侦听器默认为端口 25 上的 localhost,这与您的 Django 应用程序应使用的默认 SMTP 设置一致。

于 2021-11-07T18:29:07.293 回答