Django:PasswordResetForm连接被拒绝

时间:2015-09-11 17:30:06

标签: python django django-email

我使用了PasswordResetForm(Django的代码源代码),但我有这个错误:

[Errno 111] Connection refused
...
/usr/lib/python2.7/socket.py in create_connection, line 571
...

我的表单PasswordResetForm:https://docs.djangoproject.com/fr/1.8/_modules/django/contrib/auth/forms/#PasswordResetForm

我的观点,同上(https://docs.djangoproject.com/fr/1.8/_modules/django/contrib/auth/views/#password_reset)。

我的网址:

url(r'^parameters/password_reset','password_reset',name="password_reset"),
url(r'^parameters/password_reset_done','password_reset_done',name="password_reset_done"),
url(r'^parameters/password_reset_confirm','password_reset_confirm',name="password_reset_confirm"),
url(r'^parameters/password_reset_complete','password_reset_complete',name="password_reset_complete"),

和我的设置:

#-*- coding: utf-8 -*-

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

#AUTHENTICATION_BACKENDS = ('backend.EmailAuthBackend',) Authentification : email+password

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'the_website',
    'bootstrapform',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
   )

   ROOT_URLCONF = 'the_website.urls'

   WSGI_APPLICATION = 'the_website.wsgi.application'


# Database

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization

LANGUAGE_CODE = 'fr-FR'

TIME_ZONE = 'Europe/Paris'

USE_I18N = True

USE_L10N = True

USE_TZ = True


   # Static files (CSS, JavaScript, Images)

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

LOGIN_URL = '/the_website/'

当我在shell中测试时:

In [6]: send_mail("Test","Test","xxxx@yahoo.fr", ["yyyy@yahoo.fr"])
---------------------------------------------------------------------------
 error                                     Traceback (most recent call last)
 <ipython-input-6-0682cb627fb0> in <module>()
 ----> 1 send_mail("Test","Test","xxxx@yahoo.fr",["yyyy@yahoo.fr"])

 /usr/local/lib/python2.7/dist-packages/django/core/mail/__init__.pyc in send_mail(subject, message, from_email, recipient_list, fail_silently, auth_user, auth_password, connection, html_message)
 60         mail.attach_alternative(html_message, 'text/html')
 61 
---> 62     return mail.send()
     63 
     64 

/usr/local/lib/python2.7/dist-packages/django/core/mail/message.pyc in send(self, fail_silently)
281             # send to.
282             return 0
--> 283         return self.get_connection(fail_silently).send_messages([self])
284 
285     def attach(self, filename=None, content=None, mimetype=None):

     /usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.pyc in send_messages(self, email_messages)
 90             return
 91         with self._lock:
 ---> 92             new_conn_created = self.open()
 93             if not self.connection:
 94                 # We failed silently on open().

 /usr/local/lib/python2.7/dist-packages/django/core/mail/backends/smtp.pyc in open(self)
 48             connection_params['timeout'] = self.timeout
 49         try:
 ---> 50             self.connection = connection_class(self.host, self.port, **connection_params)
 51 
 52             # TLS/SSL are mutually exclusive, so only attempt TLS over

 /usr/lib/python2.7/smtplib.pyc in __init__(self, host, port, local_hostname, timeout)
254         self.esmtp_features = {}
255         if host:
--> 256             (code, msg) = self.connect(host, port)
257             if code != 220:
258                 raise SMTPConnectError(code, msg)

/usr/lib/python2.7/smtplib.pyc in connect(self, host, port)
314         if self.debuglevel > 0:
315             print>>stderr, 'connect:', (host, port)
--> 316         self.sock = self._get_socket(host, port, self.timeout)
317         (code, msg) = self.getreply()
318         if self.debuglevel > 0:

/usr/lib/python2.7/smtplib.pyc in _get_socket(self, host, port, timeout)
289         if self.debuglevel > 0:
290             print>>stderr, 'connect:', (host, port)
--> 291         return socket.create_connection((host, port), timeout)
292 
293     def connect(self, host='localhost', port=0):

/usr/lib/python2.7/socket.pyc in create_connection(address, timeout, source_address)
569 
570     if err is not None:
--> 571         raise err
572     else:
573         raise error("getaddrinfo returns an empty list")

error: [Errno 111] Connection refused

1 个答案:

答案 0 :(得分:0)

我知道这个问题是前一段时间提出的,但是我认为同样的问题仍然存在。 您需要做的就是向您的settings.py添加一些电子邮件配置。请参见docs。将以下所有内容添加到settings.py:

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = '<your smtp server>'
EMAIL_HOST_PASSWORD = '<your smtp password>'
EMAIL_HOST_USER = '<your smtp user email address>'
EMAIL_HOST_PORT = <your smtp port>

如果您尚未设置电子邮件服务,则可以免费链接您的Gmail帐户。 Mandrill(smtp.mandrillapp.com)和Sendgrid都是不错的Python友好资源。

我还注意到您的设置分散在forms.py,views.py,urls.py和settings.py中。可以找到更有效的设置here。它会自动配置views.py和urls.py-无需添加任何内容。只要确保正确填写了forms.py和settings.py,就可以了!