RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1217254
Accepted
Crazy Theory
Crazy Theory
Asked:2021-12-11 18:13:46 +0000 UTC2021-12-11 18:13:46 +0000 UTC 2021-12-11 18:13:46 +0000 UTC

urlsafe_base64_decode 返回无

  • 772

在我的项目中,我实现了电子邮件地址验证。

用户在表单中输入注册所需的所有数据,然后我们cleaned_data从属性中获取电子邮件地址并发送消息。

我通过以下方式实现发送(我会在解释后附上代码):

  1. 有一个继承自 FormView 的 mixin,它定义了 FormValid 方法,以便将发送消息所需的数据发送到表单的 save 方法。
  2. 我们从上面的 mixin 中继承视图并定义随后在提交中使用的字段。

这是混合:

class SendsMessageMixin(FormView):
    """
    A mixin that provides all inherited
    classes with the functionality of sending an email.
    """

    email_subject = email_template_name = token_generator = None

    def form_valid(self, form):
        """
        When this method is triggered, the data that will
        later be used to send the message is passed to the form save method.
        """

        form.save(
            **
            {
                'request': self.request,
                'use_https': self.request.is_secure(),
                'subject': self.email_subject,
                'template_name': self.email_template_name,
                'token_generator': self.token_generator
            }
        )

        return super().form_valid(form)

表现:

class Registration(UnauthorizedRequiredMixin, SendsMessageMixin):
    form_class = ConsumerCreationForm
    template_name = 'consumers/registration.html'
    email_subject = 'Confirm your account.'
    email_template_name = 'mail/verification.html'
    token_generator = default_token_generator
    success_url = reverse_lazy('message_sent')

表格保存方法:

    def save(self, **sending_data):
        """
        Saves an inactive user to the database, after sending
        a message asking for confirmation of registration to the email address.
        """

        consumer = super().save(commit=False)
        consumer.set_password(self.cleaned_data['password'])

        prepare_data_and_send_message_with_uid_and_token(consumer=consumer, **sending_data)
        consumer.save()

        return consumer

消息内容生成:

def prepare_data_and_send_message_with_uid_and_token(request, consumer, use_https, subject,
                                                     template_name, token_generator):
    """
    Prepares data to be loaded into message content,
    the main purpose of which is to send a unique link to the user.
    """

    content = {
        'domain': get_current_site(request).domain,
        'uid': urlsafe_base64_encode(force_bytes(consumer.pk)),
        'token': token_generator.make_token(consumer),
        'protocol': 'https' if use_https else 'http'
    }

    send_message(subject, template_name, content, consumer.email)

发送消息

@task()
def send_message(subject, template_name, content, to_email):
    """
    Sends a message to the specified email address in the background.
    """

    body = render_to_string(template_name, content)
    message = EmailMessage(subject, body, to=[to_email])
    message.send()

huey用于后台发送

消息本身:

{% autoescape off %}
Hello, your email address has been selected for registration!
To confirm, follow the link.
{{ protocol }}://{{ domain }}{% url 'activate' uidb64=uid token=token %}
In case you didn't do it, just ignore this message.
{% endautoescape %}

切换到生成的激活链接时,会触发dispatch方法,由于方法在这里没有发挥作用,所以一般不重要。

在此调度方法中,我从 kwargs 字典中提取 uidb64 并使用以下构造:

urlsafe_base64_decode(uidb64).decode().

它返回无。

最重要的是,无论如何都会返回 None 。我想强调一下,uidb64 是完美生成的并进入了 kwargs。

我将添加一个我试图覆盖密码恢复表单的功能(以便在后台发送消息),当我转到生成的链接时,即 PasswordResetConfirmView 视图,使用现成的逻辑,一切一声巨响,我能够恢复该帐户的密码,因此uidb收到了我的帐户(解码工作没有返回None)。

python
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Мистер Фикс
    2021-12-15T14:13:05Z2021-12-15T14:13:05Z

    我的回答:在任何情况下,urlsafe_base64_decode 函数都不能返回 None。

    要么它在您的项目中的某个地方超载,要么是 python 本身的错误(这不太可能),或者是使用的 janga 版本的错误,或者你自己走错了路。

    你在哪里看到没有?以及为什么您决定此特定函数返回 None 。请提供一个我们可以运行的示例代码,该代码将返回 None。然后就有可能争论。

    • 3
  2. Best Answer
    Crazy Theory
    2021-12-15T16:55:05Z2021-12-15T16:55:05Z

    问题是在注册表单的save方法中,也称为准备数据和发送消息的方法,我在将模型实例本身保存到数据库之前调用了它。如果检查,在发送消息之前,consumer.pk 等于 None,之后,当 save 方法起作用时,它有一个数值。感谢昵称 Mr. Fix 的人,他明确表示,如果您自己没有在那里发送 None,则永远不会返回 None。

    总而言之,这只是交换两个操作的问题。

    • 2

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5