我正在尝试向我的 Django 项目添加电子邮件确认功能,用户必须使用网站提供的链接确认它是电子邮件。
我已经添加了视图、网址和模板,但我不断收到错误消息:
Reverse for 'activate' not found. 'activate' is not a valid view function or pattern name.
1 {% autoescape off %}
2 Hi {{ user.username }},
3 Please click on the link to confirm your registration,
4 http://{{ domain }}**{% url 'activate' uidb64=uid token=token %}**
5 {% endautoescape %}
我不知道这个错误来自哪里,我想我正确定义了我的视图并将其添加到我的urls.py中,我正在关注本教程。
这是我的看法:
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
# return redirect('home')
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
return HttpResponse('Activation link is invalid!')
网址:
from .views import activate
path("activate/<slug:(?P<uidb64>[0-9A-Za-z_\-]+)>/<slug:(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$>",
views.activate, name='activate'),
还有acc_active_email.html
{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}
任何建议表示赞赏。