我正在尝试使用django基于类的视图创建密码重置功能。我已经在django中实现了所有推荐的东西但是有些东西不合适。每当我尝试点击发送密码重置电子邮件链接按钮。它会抛出一个字符串索引超出范围错误。
这是我的urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('register/',User_views.register, name='register'),
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(), 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.MEDIA_URL, document_root=settings.MEDIA_ROOT)
这是我的项目的settings.py文件 -
INSTALLED_APPS = [
'blog.apps.BlogConfig',
'users.apps.UsersConfig',
'crispy_forms',
'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 = 'django_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 = 'django_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.1/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.1/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.1/howto/static-files/
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'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_USE_SSL = True
EMAIL_HOST_USER = os.environ.get('EMAIL_USER')
EMAIL_HOST_PASSWORD = os.environ.get('EMAIL_PASSWORD')
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER
这是我的密码 - reset.html -
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Reset Password</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Request Password Reset</button>
</div>
</form>
</div>
{% endblock content %}
这是错误追溯 -
> Internal Server Error: /password-reset/ Traceback (most recent call
> last): File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py",
> line 34, in inner
> response = get_response(request) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py",
> line 126, in _get_response
> response = self.process_exception_by_middleware(e, request) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py",
> line 124, in _get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py",
> line 68, in view
> return self.dispatch(request, *args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py",
> line 45, in _wrapper
> return bound_method(*args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py",
> line 142, in _wrapped_view
> response = view_func(request, *args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\views.py",
> line 220, in dispatch
> return super().dispatch(*args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py",
> line 88, in dispatch
> return handler(request, *args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py",
> line 142, in post
> return self.form_valid(form) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\views.py",
> line 233, in form_valid
> form.save(**opts) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\forms.py",
> line 295, in save
> email, html_email_template_name=html_email_template_name, File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\forms.py",
> line 250, in send_mail
> email_message.send() File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\message.py",
> line 291, in send
> return self.get_connection(fail_silently).send_messages([self]) File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py",
> line 110, in send_messages
> sent = self._send(message) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py",
> line 122, in _send
> from_email = sanitize_address(email_message.from_email, encoding) File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\message.py",
> line 119, in sanitize_address
> address = Address(nm, addr_spec=addr) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\email\headerregistry.py",
> line 42, in __init__
> a_s, rest = parser.get_addr_spec(addr_spec) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\email\_header_value_parser.py",
> line 1582, in get_addr_spec
> token, value = get_local_part(value) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\email\_header_value_parser.py",
> line 1394, in get_local_part
> if value[0] in CFWS_LEADER: IndexError: string index out of range [24/Apr/2019 13:51:36] "POST /password-reset/ HTTP/1.1" 500 135700
> [24/Apr/2019 14:01:39] "GET /profile/ HTTP/1.1" 200 5203 [24/Apr/2019
> 14:01:52] "GET /password-reset/ HTTP/1.1" 200 4035 Internal Server
> Error: /password-reset/ Traceback (most recent call last): File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py",
> line 34, in inner
> response = get_response(request) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py",
> line 126, in _get_response
> response = self.process_exception_by_middleware(e, request) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py",
> line 124, in _get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py",
> line 68, in view
> return self.dispatch(request, *args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py",
> line 45, in _wrapper
> return bound_method(*args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\decorators.py",
> line 142, in _wrapped_view
> response = view_func(request, *args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\views.py",
> line 220, in dispatch
> return super().dispatch(*args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\base.py",
> line 88, in dispatch
> return handler(request, *args, **kwargs) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\views\generic\edit.py",
> line 142, in post
> return self.form_valid(form) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\views.py",
> line 233, in form_valid
> form.save(**opts) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\forms.py",
> line 295, in save
> email, html_email_template_name=html_email_template_name, File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\forms.py",
> line 250, in send_mail
> email_message.send() File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\message.py",
> line 291, in send
> return self.get_connection(fail_silently).send_messages([self]) File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py",
> line 110, in send_messages
> sent = self._send(message) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\backends\smtp.py",
> line 122, in _send
> from_email = sanitize_address(email_message.from_email, encoding) File
> "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\mail\message.py",
> line 119, in sanitize_address
> address = Address(nm, addr_spec=addr) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\email\headerregistry.py",
> line 42, in __init__
> a_s, rest = parser.get_addr_spec(addr_spec) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\email\_header_value_parser.py",
> line 1582, in get_addr_spec
> token, value = get_local_part(value) File "C:\Users\DELL\AppData\Local\Programs\Python\Python37-32\lib\email\_header_value_parser.py",
> line 1394, in get_local_part
> if value[0] in CFWS_LEADER: IndexError: string index out of range
我对您的观点感到困惑,这里是我对更改密码的看法
class PasswordResetView(PasswordContextMixin, FormView):
email_template_name = 'registration/password_reset_email.html'
extra_email_context = None
form_class = PasswordResetForm
from_email = None
html_email_template_name = None
subject_template_name = 'registration/password_reset_subject.txt'
success_url = reverse_lazy('password_reset_done')
template_name = 'registration/password_reset_form.html'
title = ('Password reset')
token_generator = default_token_generator
@method_decorator(csrf_protect)
def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
def form_valid(self, form):
opts = {
'use_https': self.request.is_secure(),
'token_generator': self.token_generator,
'from_email': self.from_email,
'email_template_name': self.email_template_name,
'subject_template_name': self.subject_template_name,
'request': self.request,
'html_email_template_name': self.html_email_template_name,
'extra_email_context': self.extra_email_context,
}
form.save(**opts)
return super().form_valid(form)
INTERNAL_RESET_URL_TOKEN = 'set-password'
INTERNAL_RESET_SESSION_TOKEN = '_password_reset_token'
UPDATED :
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "[email protected]"
EMAIL_HOST_PASSWORD = "<Your Passcode Here>"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
you need do account access enabled