【问题标题】:User Account activation email using Django使用 Django 的用户帐户激活电子邮件
【发布时间】:2020-02-15 03:05:43
【问题描述】:

我正在尝试创建用户注册表单。用户帐户将通过激活电子邮件激活。 出现以下错误:-

异常类型:/register/ 处的 NoReverseMatch 异常值:未找到“激活”的反向。 'activate' 不是有效的视图函数或模式名称。

我点击了以下链接:

https://studygyaan.com/django/how-to-signup-user-and-send-confirmation-email-in-django

https://blog.hlab.tech/part-ii-how-to-sign-up-user-and-send-confirmation-email-in-django-2-1-and-python-3-6/

https://medium.com/@frfahim/django-registration-with-confirmation-email-bb5da011e4ef

def register(request):
    if request.method == 'POST':
        form = NewUserForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            mail_subject = 'Activate your blog account.'
            message = render_to_string('website/acc_active_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user=user),
            })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(
                mail_subject, message, to=[to_email]
            )
            email.send()
            return HttpResponse('Please confirm your email address to complete the registration')
        else:
            for msg in form.error_messages:
                print(form.error_messages[msg])
    else:
        print('in else')
        form = NewUserForm()
    return render(request=request, template_name='website/register.html', context={'form': form})

def activate_account(request, uidb64, token):
    try:
        uid = force_bytes(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 HttpResponse('Your account has been activate successfully')
    else:
        return HttpResponse('Activation link is invalid!')

我的html文件代码

{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

【问题讨论】:

  • 你能发布你的 urls.py 吗?

标签: python django


【解决方案1】:

引发错误的代码部分似乎丢失了。

异常类型:/register/ 处的 NoReverseMatch 异常值:Reverse 找不到“激活”。 'activate' 不是有效的视图函数或 模式名称。

此错误表示您的 urls.py 中没有名称='activate'的网址。

在 django 2.x 中应该是这样的:

urlpatterns = [
    #...
    path('/register/', activate_account, name='activate'),
    #...
]

在 django 1.x 中应该是这样的:

urlpatterns = [
    #...
    url(r'^register/$', activate_account, name='activate'),
    #...
]

正如django manual 中所写,您必须为该 url 指定一个名称才能使用 reverse() 查找来访问它。

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 1970-01-01
    • 2016-03-26
    • 2012-04-27
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多