【发布时间】:2021-04-21 06:37:52
【问题描述】:
我正在使用 django 内置函数来重置密码,以防用户忘记密码。
我还创建了自己的 Html 页面以使外观看起来更好。
用于电子邮件的password_reset_form.html、password_reset_done.html、password_reset_confirm.html、password_reset_complete.html 和password_reset_email.html。
- 我的 URL 中 password_reset_form.html 的名称是“reset_password”\
- 我的 URL 中 password_reset_done.html 的名称是“password_reset_done”\
- 我的 URL 中 password_reset_confirm.html 的名称是“password_reset_done”\
- 我的 URL 中 password_reset_complete.html 的名称是“password_reset_complete”
reset_password 和 password_reset_done django 内置模板适用于我创建的 html 页面。
问题出现当我点击发送到我的电子邮件的链接时, 我应该在浏览器中看到我的自定义 url 页面(password_reset_confirm.html),但我看到的是 django 内置模板(“密码重置确认”)。
我不知道错在哪里。我已经尝试设置了 2 天,但很难弄清楚。
你能帮帮我吗?
这是我的设置:
"""
Django settings for grouppublishingindia project.
Generated by 'django-admin startproject' using Django 3.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '5r0zcv0wdd2yhow_t)k)xj$c(3mnkqiww_yr0w7k+i+ii36)5v'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'Inventory',
'crispy_forms',
'debug_toolbar',
'import_export',
]
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',
'debug_toolbar.middleware.DebugToolbarMiddleware'
]
CRISPY_TEMPLATE_PACK = "bootstrap4"
ROOT_URLCONF = 'grouppublishingindia.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'Inventory', 'templates')],
'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 = 'grouppublishingindia.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.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/3.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/3.1/howto/static-files/
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = "contenu"
#SMTP (Simple Mail Transfert Protcole) Configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '*******************'
EMAIL_HOST_PASSWORD = '***************'
这是我的项目网址:
from django.urls import path, include
from django.contrib.auth import views as auth_views
from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from Inventory import views
urlpatterns = [
path('admin/', admin.site.urls),
path("",include("Inventory.urls")),
url('Inventory/',include('django.contrib.auth.urls')),
]
这是我的应用网址:
from django.contrib.auth import views as auth_views
from django.urls import reverse
from django.conf.urls import url
from .views import *
from Inventory import views
from .views import index
from Inventory.views import SearchResultsView
from django.urls import path
app_name = "Inventory"
urlpatterns =[
path('password_reset/', auth_views.PasswordResetView.as_view(template_name='Inventory/password_reset_form.html',
success_url=reverse_lazy('Inventory:password_reset_done')),name="reset_password"
),
path('password_reset/done/',
auth_views.PasswordResetDoneView.as_view(template_name='Inventory/password_reset_done.html'), name="password_reset_done"
),
path('password_reset_confirm/<uidb64>/<token>/',
auth_views.PasswordResetConfirmView.as_view(template_name='Inventory/password_reset_confirm.html'),
name="password_reset_confirm"
),
path('reset_password_complete/',
auth_views.PasswordResetCompleteView.as_view(template_name='Inventory/password_reset_complete.html'),
name="password_reset_complete"
),
]
【问题讨论】:
-
在将 url 名称从
password_reset_confirm重命名为password_reset_confirmation后再次尝试。因为password_reset_comfirm可能被用作 django 内置 url 的名称。 -
@AjayLingayat,感谢您的快速回复,我试过了,但还是不行
-
@Ajay Lingayat,我通过再次浏览找到了答案。错误位于主 URL 路径(项目 url)中。我没有将我的自定义 html URL 放在主项目 url 路径中。所以我改变了路径,然后我得到了预期的结果。我提到了错误的位置,以防我的回复可以帮助某人。 :)