gpt4 book ai didi

python - Django 2、python 3.4 无法解码 urlsafe_base64_decode(uidb64)

转载 作者:太空狗 更新时间:2023-10-29 17:21:35 24 4
gpt4 key购买 nike

我正在尝试通过电子邮件激活用户,电子邮件有效,编码有效,我使用了 django1.11 中的一种方法,该方法运行成功。

在 Django 1.11 中,以下代码成功解码为 28,其中 uidb64 = b'Mjg'

force_text(urlsafe_base64_decode(uidb64))

在 django 2 (2, 0, 0, 'final', 0) 上面的代码解码不工作并导致错误

django.utils.encoding.DjangoUnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 1: invalid continuation byte. You passed in b'l\xc8\xe0' (<class 'bytes'>)

我也发表一下我的看法以防万一

from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
# auth_login(request, user)
message = render_to_string('user_activate_email.html', {
'user': user,
'domain': Site.objects.get_current().domain,
'uidb64': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
mail_subject = 'Activate your blog account.'
to_email = form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
messages.info(
request, 'Activation link has been sent to your email!')
# return redirect('home')
return render(request, 'index.html')
else:
form = SignUpForm()
return render(request, 'user_action.html', {'form': form})


def activate(request, uidb64, token):
try:
import pdb;
pdb.set_trace()
uid = urlsafe_base64_decode(uidb64).decode()
user = User.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError):
user = None
if user is not None and account_activation_token.check_token(user, token):

user.refresh_from_db()
user.is_active = True

user.save()
auth_login(request, user)
messages.info(request, 'Your account has been activated successfully!')
return redirect('events:home')
else:
messages.info(
request, 'Activation link is invalid or has been activated')
return redirect('events:home')

PS:这只是我使用 CBV 之前的试用。

编辑:包括回溯

System check identified no issues (0 silenced).
December 15, 2017 - 05:51:01
Django version 2.0, using settings 'event_management.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
> /home/vipinmohan/django2-0/event/event_management/users/views.py(88)activate()
-> uid = urlsafe_base64_decode(uidb64).decode()
(Pdb) n
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcc in position 1: invalid continuation byte
> /home/vipinmohan/django2-0/event/event_management/users/views.py(88)activate()
-> uid = urlsafe_base64_decode(uidb64).decode()
(Pdb) n
> /home/vipinmohan/django2-0/event/event_management/users/views.py(90)activate()
-> except(TypeError, ValueError, OverflowError):
(Pdb) n
> /home/vipinmohan/django2-0/event/event_management/users/views.py(91)activate()
-> user = None
(Pdb)

最佳答案

好的。经过一个小时的搜索(仍然是 python django 的初学者),发行说明中指出了一个相关的变化,其定义对新手来说并不困难。 https://docs.djangoproject.com/en/2.0/releases/2.0/#removed-support-for-bytestrings-in-some-places

To support native Python 2 strings, older Django versions had to accept both bytestrings and unicode strings. Now that Python 2 support is dropped, bytestrings should only be encountered around input/output boundaries (handling of binary fields or HTTP streams, for example). You might have to update your code to limit bytestring usage to a minimum, as Django no longer accepts bytestrings in certain code paths.

For example, reverse() now uses str() instead of force_text() to coerce the args and kwargs it receives, prior to their placement in the URL. For bytestrings, this creates a string with an undesired b prefix as well as additional quotes (str(b'foo') is "b'foo'"). To adapt, call decode() on the bytestring before passing it to reverse().

完全无法理解它的含义,我不得不更深入地研究实际的 django 核心代码。

所以从django 1.11到2.0编码变化如下

'uid': urlsafe_base64_encode(force_bytes(user.pk)),

'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),

解码自

uid = force_text(urlsafe_base64_decode(uidb64))

 uid = urlsafe_base64_decode(uidb64).decode()

就是这样。希望这对某人有帮助。

***************编辑******************

从 Django 2.2 开始

django.utils.http.urlsafe_base64_encode() 现在返回字符串而不是字节串。

django.utils.http.urlsafe_base64_decode() 可能不再传递字节串。

感谢 Hillmark 指出这一点

关于python - Django 2、python 3.4 无法解码 urlsafe_base64_decode(uidb64),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47814228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com