Я новичок в django. Я работаю над сбросом пароля с помощью PasswordResetView. Я сделал все необходимые настройки. Все представления работают правильно (сбросить, выполнить, подтвердить, завершить), но электронное письмо не отправляется. Я использую файловый метод. Содержимое почты также успешно сохраняется в текстовом файле. Текстовый файл
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: Password reset on 127.0.0.1:8000
From: webmaster@localhost
To: user1991@gmail.com
Date: Fri, 08 May 2020 13:07:44 -0000
Message-ID:
<158894326451.7060.3441580324173819052@DESKTOP-G46QTJT.localdomain>
You're receiving this email because you requested a password reset for your user account at 127.0.0.1:8000.
Please go to the following page and choose a new password:
http://127.0.0.1:8000/accounts/profile/reset-password/confirm/Ng/5gb-4468fc4f5f5c9ad67d90/
Your username, in case you’ve forgotten: test@5
Thanks for using our site!
The 127.0.0.1:8000 team
-------------------------------------------------------------------------------
Но я не получил письмо. Надеюсь на решение. По крайней мере, я хочу знать, как сгенерировать только одноразовый URL-адрес, такой как django, созданный автоматически.
settings.py
"""
Django settings for tutorial project.
Generated by 'django-admin startproject' using Django 3.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/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/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '!ku^2ct1u^jmk3+_8emila*@r2(9)avhsgf1@rnfhxb=30^4bp'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'accounts',
'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',
'tutorial.middleware.LoginRequiredMiddleware',
]
ROOT_URLCONF = 'tutorial.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 = 'tutorial.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/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/3.0/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/3.0/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/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ]
LOGIN_REDIRECT_URL = '/accounts/profile'
LOGIN_URL = '/accounts/login/'
LOGIN_EXEMPT_URLS = [
r'accounts/register',
]
EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"
EMAIL_FILE_PATH = os.path.join(BASE_DIR, "sent_emails")
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'projectkesamad@gmail.com'
EMAIL_HOST_PASSWORD = 'Kesavan@pro'
urls.py
from django.urls import path, reverse_lazy, reverse
from . import views
from django.contrib.auth.views import (
LoginView,LogoutView,PasswordResetView,PasswordResetDoneView,PasswordResetConfirmView,PasswordResetCompleteView
)
app_name='accounts'
urlpatterns=[
path('',views.home,name='accounts_home_page'),
path('login/',LoginView.as_view(template_name='accounts/login.html'),name='login_page'),
path('logout/',LogoutView.as_view(template_name='accounts/logout.html'),name='logout_page'),
path('register/',views.registration,name='register_page'),
path('profile/',views.profile,name='profile'),
path('profile/edit_profile/',views.edit_profile,name='edit-profile'),
path('profile/change-password/',views.change_password,name='edit-profile'),
path('profile/reset-password/',PasswordResetView.as_view(template_name='accounts/reset_password.html',success_url = reverse_lazy('accounts:password_reset_done')),name='password_reset'),
path('profile/reset-password/done/',PasswordResetDoneView.as_view(),name='password_reset_done'),
path('profile/reset-password/confirm/<uidb64>/<token>/',PasswordResetConfirmView.as_view(success_url = reverse_lazy('accounts:password_reset_complete')),name='password_reset_confirm'),
path('profile/reset-password/complete/',PasswordResetCompleteView.as_view(),name='password_reset_complete'),
]