python - Dango 2.2 使用关键字参数反转 'activate'

标签 python django email path

我在创建帐户并使用电子邮件中发送的链接激活它时遇到问题。该应用程序是用 Django 2.2 版编写的。

点击激活链接后,我收到一条消息:
Reverse for 'activate' with keyword arguments '{'uidb64': '', 'token': ''}' not found. 1 pattern(s) tried: ['activate/(?P<uidb64>[^/]+)/(?P<token>[^/]+)/$']
urls.py 中的代码
path('activate/<uidb64>/<token>/', account.activate, name='activate'),
views.py 中的代码,用于注册和激活链接的代码。注册就像一个 CBV,而激活就像一个 FBV。

class Signup(View):
    def get(self, request):
        form = SignUpForm()
        return render(request, 'account/signup.html', {'form': form})

    def post(self, request):
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Activate your Spotted account'
            message = render_to_string('account/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user)
            })
            user.email_user(subject, message)
            return redirect('account_activation_sent')
        return render(request, 'account/signup.html', {'form': form})


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.profile.email_confirmed = True
        user.save()
        login(request, user)
        return render(request, 'account/account_activation_email.html')
    else:
        return render(request, 'account/account_activation_invalid.html')

在 account/account_activation_email.html 中:
{% autoescape off %}
Hi {{ user.username }},

Please click on the link below to confirm your registration:

http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

token 文件
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six


class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (
            six.text_type(user.pk) + six.text_type(timestamp) +
            six.text_type(user.profile.email_confirmed)
        )


account_activation_token = AccountActivationTokenGenerator()

最佳答案

如果您使用的是 Django 3.1 或更高版本,则激活/密码重置机制对 token 使用 SHA-256 散列算法。
你的正则表达式太严格了。
根据文档,您应该使用路径而不是 url,如下所示。

path('reset/<uidb64>/<token>/', ...)

path('activate/<uidb64>/<token>/', ...)
详情here

关于python - Dango 2.2 使用关键字参数反转 'activate',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59570212/

相关文章:

python - 究竟应该在 PYTHONPATH 中设置什么?

python - 检索页面中所有外部对象的 URL,包括。动态加载

macos - Mavericks 中的 Apple Mail : Any way to change MinimumHTMLFontSize?

python - Django Zappa 部署中的包装器错误

python - python虚拟环境出错

c# - 在我终止我的应用程序之前,邮件不会发送 - System.Mail.Net

PHPMailer 和 Gmail 对话 View

python - 相互排斥的论点

Python 统计一秒钟有多少次点击

python - 如何在 python/django 中使用 URL 保存图像