Django Wagtail [错误 111] 连接拒绝表单提交

Django Wagtail [Errno 111] Connection refused form submission

我有一个使用 Wagtail (Django) 制作的网站,并设置了一个带有登陆页面的联系表。在本地主机上使用此联系表时,我可以成功地在联系表上添加信息并提交,这会将我带到登录页面。

但是,我已经在 heroku 上托管了它,现在提交联系表时遇到了问题。当我提交联系表格时,它给我一个连接被拒绝的错误 [Errno 111]:

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/wagtail/core/views.py", line 24, in serve
    return page.serve(request, *args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/wagtail/contrib/forms/models.py", line 300, in serve
    form_submission = self.process_form_submission(form)
  File "/app/.heroku/python/lib/python3.8/site-packages/wagtail/contrib/forms/models.py", line 341, in process_form_submission
    self.send_mail(form)
  File "/app/.heroku/python/lib/python3.8/site-packages/wagtail/contrib/forms/models.py", line 346, in send_mail
    send_mail(self.subject, self.render_email(form), addresses, self.from_address,)
  File "/app/.heroku/python/lib/python3.8/site-packages/wagtail/admin/mail.py", line 62, in send_mail
    return mail.send()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/mail/message.py", line 284, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 102, in send_messages
    new_conn_created = self.open()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/mail/backends/smtp.py", line 62, in open
    self.connection = self.connection_class(self.host, self.port, **connection_params)
  File "/app/.heroku/python/lib/python3.8/smtplib.py", line 253, in __init__
    (code, msg) = self.connect(host, port)
  File "/app/.heroku/python/lib/python3.8/smtplib.py", line 339, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/app/.heroku/python/lib/python3.8/smtplib.py", line 308, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "/app/.heroku/python/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/app/.heroku/python/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)

Exception Type: ConnectionRefusedError at /contact/
Exception Value: [Errno 111] Connection refused

我不确定是否因为电子邮件设置不正确而发生此错误,或者它是否在重定向到着陆页时正在寻找本地主机。

这是我的环境:

Django Version: 3.1.5
Python Version: 3.8.5
Installed Applications:
['home',
 'search',
 'flex',
 'streams',
 'site_settings',
 'subscribers',
 'blogsite',
 'projects',
 'contact',
 'wagtail.contrib.forms',
 'wagtail.contrib.redirects',
 'wagtail.contrib.settings',
 'wagtail.contrib.routable_page',
 'wagtail.contrib.sitemaps',
 'wagtail.contrib.modeladmin',
 'wagtail.embeds',
 'wagtail.sites',
 'wagtail.users',
 'wagtail.snippets',
 'wagtail.documents',
 'wagtail.images',
 'wagtail.search',
 'wagtail.admin',
 'wagtail.core',
 'modelcluster',
 'taggit',
 'djrichtextfield',
 'wagtailfontawesome',
 'wagtailcodeblock',
 'wagtailuiplus',
 'wagalytics',
 'wagtail_blocks',
 'wagtailstreamforms',
 'wagtail_color_panel',
 'wagtailcaptcha',
 'widget_tweaks',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sitemaps']
Installed Middleware:
['django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'wagtail.contrib.redirects.middleware.RedirectMiddleware']

显示本地主机的回溯详细信息之一的示例。我不确定 Heroku 是否通过其自己的本地主机工作,或者这是否是错误的来源:

有趣的是,表单中的数据被捕获并显示在我的管理表单数据页面中:

知道哪里出了问题吗?

发送电子邮件通知失败,因为您没有 SMTP 后端。您需要配置一个 - 请参阅

https://docs.djangoproject.com/en/3.1/topics/email/#smtp-backend

Heroku 可以轻松连接一系列 SMTP 提供商:

https://devcenter.heroku.com/articles/smtp

我们使用 Mailgun,它有一个慷慨的免费套餐:

https://elements.heroku.com/addons/mailgun

如果你不关心发送电子邮件,你可以将它们推送到控制台后端:

https://docs.djangoproject.com/en/3.1/topics/email/#console-backend