Django密码重置。不发送邮件

时间:2022-06-01 19:22:02

I'm trying to get the django password reset working but the reset email does not get sent.

我正在尝试让django密码重置工作,但重置电子邮件不会被发送。

I know my email is properly configured because the following works both in the shell and in one of my views (I'm using it to get support email to myself).

我知道我的电子邮件配置正确,因为以下内容适用于shell和我的一个视图(我用它来获取支持电子邮件给自己)。

from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.',
          'admin@mydomain.com',['me@gmail.com'], fail_silently=False)

I can get to my reset password view (password/reset/) and after I give it my email it correctly redirects me to password/reset/done/ but it doesn't sends the email.

我可以进入我的重置密码视图(密码/重置/),在我给它发送电子邮件后,它正确地重定向到密码/重置/完成/但它不会发送电子邮件。

Here's my urls.py:

这是我的urls.py:

(r'^password/reset/$','django.contrib.auth.views.password_reset'),
(r'^password/reset/done/$','django.contrib.auth.views.password_reset_done'),
(r'^password/reset/confirm/$','django.contrib.auth.views.password_reset_confirm'),
(r'^password/reset/complete/$','django.contrib.auth.views.password_reset_confirm'),
(r'^password/change/$','django.contrib.auth.views.password_change'),
(r'^password/change/done/$','django.contrib.auth.views.password_change_done'),

Here's my password_reset_form.html:

这是我的password_reset_form.html:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="/media/css/style_login.css" />
    <title>Información de acceso requerida</title>
</head>
<body>
    <div id="wrapper">
        <h1>Recuperar password</h1>
        <p>Utilice este formulario cuando desee recuperar el password para su usuario.</p>
        {% if form.errors %}
        <p>No hay un usuario registrado con ese correo electronico.</p>
        {% endif %}
        <form method="post" action="{% url django.contrib.auth.views.password_reset_done %}">
            {% csrf_token %}
            {{ form }}
            <input class="login" type="submit" value="Recuperar" />
        </form>
    </div>
</body>

Any ideas? Thanks

有任何想法吗?谢谢

2 个解决方案

#1


8  

I should have mention I'm using hostgator as my provider. So this is my settings.py

我应该提到我使用hostgator作为我的提供者。所以这是我的settings.py

EMAIL_HOST      = 'my-domain.com'
EMAIL_HOST_PASSWORD = 'my cpanel password'
EMAIL_HOST_USER = 'my cpanel user'
EMAIL_PORT      = 25
EMAIL_USE_TLS   = False
DEFAULT_FROM_EMAIL  = 'webmaster@my-host.com'
SERVER_EMAIL    = 'root@my-domain.com'

The above settings work!

以上设置有效!

#2


5  

In my case I created the users using create_user(). My idea was to create a bunch of accounts and then tell people they could go to the password reset form to set their password. I think it sucks if you need to tell them 'Use password "welcome123" and then don't forget to modify your password' or such.

在我的例子中,我使用create_user()创建了用户。我的想法是创建一堆帐户然后告诉人们他们可以转到密码重置表单来设置他们的密码。如果您需要告诉他们“使用密码”welcome123“然后别忘了修改您的密码”,我认为这很糟糕。

I found out if I did not pass Password='foo' or also if I passed Password=None to create_user() password resets are not sent. This is because Django sometimes uses an empty password to know that it does not need to authenticate locally, but rather use LDAP or such.

我发现如果我没有传递Password ='foo',或者如果我传递Password = None到create_user(),则不会发送密码重置。这是因为Django有时使用空密码来知道它不需要在本地进行身份验证,而是使用LDAP等。

So now I do this:

所以现在我这样做:

User.objects.create_user(
  user_name,
  email=user_email,
  password=os.urandom(32),
)

And I can direct people to use the password reset function for their new accounts. I think it would be awesome if I had an option in create_user to automatically send emails for account activation to users, though!

我可以指示人们为他们的新帐户使用密码重置功能。我认为如果我在create_user中有一个选项可以自动发送电子邮件以激活用户帐户,那将是非常棒的!

#1


8  

I should have mention I'm using hostgator as my provider. So this is my settings.py

我应该提到我使用hostgator作为我的提供者。所以这是我的settings.py

EMAIL_HOST      = 'my-domain.com'
EMAIL_HOST_PASSWORD = 'my cpanel password'
EMAIL_HOST_USER = 'my cpanel user'
EMAIL_PORT      = 25
EMAIL_USE_TLS   = False
DEFAULT_FROM_EMAIL  = 'webmaster@my-host.com'
SERVER_EMAIL    = 'root@my-domain.com'

The above settings work!

以上设置有效!

#2


5  

In my case I created the users using create_user(). My idea was to create a bunch of accounts and then tell people they could go to the password reset form to set their password. I think it sucks if you need to tell them 'Use password "welcome123" and then don't forget to modify your password' or such.

在我的例子中,我使用create_user()创建了用户。我的想法是创建一堆帐户然后告诉人们他们可以转到密码重置表单来设置他们的密码。如果您需要告诉他们“使用密码”welcome123“然后别忘了修改您的密码”,我认为这很糟糕。

I found out if I did not pass Password='foo' or also if I passed Password=None to create_user() password resets are not sent. This is because Django sometimes uses an empty password to know that it does not need to authenticate locally, but rather use LDAP or such.

我发现如果我没有传递Password ='foo',或者如果我传递Password = None到create_user(),则不会发送密码重置。这是因为Django有时使用空密码来知道它不需要在本地进行身份验证,而是使用LDAP等。

So now I do this:

所以现在我这样做:

User.objects.create_user(
  user_name,
  email=user_email,
  password=os.urandom(32),
)

And I can direct people to use the password reset function for their new accounts. I think it would be awesome if I had an option in create_user to automatically send emails for account activation to users, though!

我可以指示人们为他们的新帐户使用密码重置功能。我认为如果我在create_user中有一个选项可以自动发送电子邮件以激活用户帐户,那将是非常棒的!