我正试图在我的Django项目中添加电子邮件确认功能,用户必须使用该网站提供的链接来确认电子邮件。
我已经添加了视图,URL和模板,但是我不断收到错误消息:
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 中,我正在关注this tutorial。>
这是我的观点:
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 %}
任何建议都值得赞赏。