gpt4 book ai didi

python - Django-registration 重新发送带有新代码的激活邮件

转载 作者:太空狗 更新时间:2023-10-29 21:54:36 25 4
gpt4 key购买 nike

我正在使用 django-registration具有唯一的电子邮件地址,如果用户请求并且该帐户尚未激活,我希望能够将第二个激活表格发送到电子邮件。我找到了这个 link在 Stackoverflow 上,但我不明白我应该把例程放在哪里。我已将其放入我的应用程序的 views.py 中,但出现错误

global name 'RegistrationProfile' is not defined

这是因为 RegistrationProfile 是一个注册模型,但在发送激活表单时它不会在我的数据库中创建表/记录,所以我不明白如何访问它。

欢迎任何建议。

最佳答案

好的,这就是我想出的解决方案。它可能不是最漂亮的,但它对我有用。

这被添加到 views.py

from .forms import ResendActivationEmailForm
from django.core import signing
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string


def resend_activation_email(request):

email_body_template = 'registration/activation_email.txt'
email_subject_template = 'registration/activation_email_subject.txt'

if not request.user.is_anonymous():
return HttpResponseRedirect('/')

context = Context()

form = None
if request.method == 'POST':
form = ResendActivationEmailForm(request.POST)
if form.is_valid():
email = form.cleaned_data["email"]
users = User.objects.filter(email=email, is_active=0)

if not users.count():
form._errors["email"] = ["Account for email address is not registered or already activated."]

REGISTRATION_SALT = getattr(settings, 'REGISTRATION_SALT', 'registration')
for user in users:
activation_key = signing.dumps(
obj=getattr(user, user.USERNAME_FIELD),
salt=REGISTRATION_SALT,
)
context = {}
context['activation_key'] = activation_key
context['expiration_days'] = settings.ACCOUNT_ACTIVATION_DAYS
context['site'] = get_current_site(request)

subject = render_to_string(email_subject_template,
context)
# Force subject to a single line to avoid header-injection
# issues.
subject = ''.join(subject.splitlines())
message = render_to_string(email_body_template,
context)
user.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
return render(request, 'registration/resend_activation_email_done.html')

if not form:
form = ResendActivationEmailForm()

context.update({"form" : form})
return render(request, 'registration/resend_activation_email_form.html', context)

这被添加到forms.py

class ResendActivationEmailForm(forms.Form):
email = forms.EmailField(required=True)

我在注册时有一个名为 resend_activation_email_form.html 的新模板,当重新发送电子邮件时使用它,但我使用完全相同的模板发送电子邮件。

我基于电子邮件,因为我使用的是唯一的电子邮件,但基于用户名可能更明智,因为 django-registration 将其定义为唯一。

关于python - Django-registration 重新发送带有新代码的激活邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37302934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com