我正在尝试使 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