gpt4 book ai didi

django - 为什么我收到这个错误?属性错误 : 'str' object has no attribute 'decode'

转载 作者:行者123 更新时间:2023-12-04 12:17:13 28 4
gpt4 key购买 nike

我正在尝试创建一个带有电子邮件验证的注册页面。我是 Python Dajngo 网络开发的新手。目前我使用 Python 3.6、Django 2.2.4、Postgresql 11 和 Ubuntu OS。但是我遇到了问题,无法弄清楚。各位大侠能帮我解决一下问题吗?提前致谢!

我提供了以下所有代码:

这是我得到的错误:

AttributeError at /register/
'str' object has no attribute 'decode'
Request Method: POST
Request URL: http://127.0.0.1:8000/register/
Django Version: 2.2.4
Exception Type: AttributeError
Exception Value:
'str' object has no attribute 'decode'
Exception Location: /media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/chhichhi_project/users/views.py in register, line 36
Python Executable: /media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/env/chhichhi/bin/python
Python Version: 3.6.8
Python Path:
['/media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/chhichhi_project',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/media/coduser/KAPTRANS/ProgrammingProj-2/test/ChhichhiProject_comment_done/ChhichhiProject/env/chhichhi/lib/python3.6/site-packages']
Server time: Wed, 28 Aug 2019 09:37:05 +0000

这是 settings.py 文件
"""
Django settings for chhichhi_project project.

Generated by 'django-admin startproject' using Django 2.2.3.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "2ba!+2akp+w%d_dfhj)u_@+rg&t8)r$&uyfwza+cza4jv55cyr"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'users.apps.UsersConfig', # new
'comments.apps.CommentsConfig', # new
'blog.apps.BlogConfig', # new
'crispy_forms', # 3rd party form styler
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'chhichhi_project.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'chhichhi_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'cicidb1',
'USER': 'postgres',
'PASSWORD': '1324pass',
'HOST': 'localhost',
'PORT': '5432',
}
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'blog/static/')
]

STATIC_ROOT= os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

CRISPY_TEMPLATE_PACK = 'bootstrap4'

LOGIN_REDIRECT_URL = 'blog_home'
LOGIN_URL = 'login'


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# my custom local host mail server
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'youremail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
# my custom local host mail server
# EMAIL_HOST = 'smtp.gmail.com'
# EMAIL_PORT = 465
# EMAIL_HOST_USER = 'youremail@gmail.com'
# EMAIL_HOST_PASSWORD = 'yourpassword'
# EMAIL_USE_TLS = False
# EMAIL_USE_SSL = True


# END custom mail server

这是 views.py 文件
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required

# Email verification
from django.http import HttpResponse
# from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
# from .forms import UserSignUpForm
from .forms import UserRegisterForm, UserUpdateForm, ProfileUpdateForm
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode,
urlsafe_base64_decode
from django.template.loader import render_to_string
from .token_generator import account_activation_token
from django.contrib.auth.models import User
from django.core.mail import EmailMessage
# Email verificatin end


def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
email_subject = 'Activate Your Account'
message = render_to_string('activate_account.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
to_email = form.cleaned_data.get('email')
email = EmailMessage(email_subject, message, to=[to_email])
email.send()
return HttpResponse('We have sent you an email, please confirm your email address to complete registration')
# username = form.cleaned_data.get('username')
# messages.success(request, f'Your account has been created! You are now able to log in.')
# return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'users/register.html',{'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!')

这是 urls.py 文件
from django.conf.urls import url
from . import views


urlpatterns = [
url(r'^$', views.register, name='register_user'),
url(r'^activate/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
views.activate_account, name='activate'),
]

这是 token_generator.py 文件
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six


class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) + six.text_type(user.is_active)
)


account_activation_token = TokenGenerator()

这是主要的 urls.py 文件
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from users import views as user_views

urlpatterns = [
path('admin/', admin.site.urls),
# path('register/', user_views.register, name='register'),
path('register/', include('users.urls')), # new url
path('profile/', user_views.profile, name='profile'),
path('login/', auth_views.LoginView.as_view(template_name = 'users/login.html'), name='login'),
path('logout/', auth_views.LogoutView.as_view(template_name = 'users/logout.html'), name='logout'),
path('password-reset/',
auth_views.PasswordResetView.as_view(template_name = 'users/password_reset.html'),
name='password_reset'),
path('password-reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name = 'users/password_reset_done.html'),
name='password_reset_done'),
path('password-reset-confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name = 'users/password_reset_confirm.html'),
name='password_reset_confirm'),
path('password-reset-complete/',
auth_views.PasswordResetCompleteView.as_view(template_name = 'users/password_reset_complete.html'),
name='password_reset_complete'),
path('', include('blog.urls')),

]

if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

这是 register.html 文件
{% extends 'blog/base.html' %}
{% load crispy_forms_tags %}
{% block title %}Registration{% endblock title %}

{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Sign Up</button>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Already Have An Account? <a class="ml-2" href="{% url 'login' %}">Sign In</a>
</small>
</div>
</div>
{% endblock content %}

最佳答案

你应该学会阅读你的错误跟踪,通常它们会准确地告诉你发生了什么。

在这种情况下,错误位置( Exception Location )是行

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

错误是 str类型没有属性 decode , 意思是
urlsafe_base64_encode(force_bytes(user.pk))

显然是 str , 不是字节字符串,因此您不能应用该方法 decode到它。下一步是检查 documentation for urlsafe_base64_encode你会在 Django 2.2 中读到,这将返回 str .

伟大的!因此,使用 Django 2.2,无需解码以前的 bytestring , 只需传递您从 urlsafe_base64_encode 获得的字符串直接发送到您的邮件模板。

关于django - 为什么我收到这个错误?属性错误 : 'str' object has no attribute 'decode' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57689928/

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