在django中生成token

问题描述 投票:0回答:1

我对django很陌生。我正在使用PasswordResetView来重置密码。我已经做了所有必要的配置,所有的视图都在正常工作(重置,完成,确认,完成),但是邮件没有发送。所有的视图都正常工作(reset,doed,confirm,complete),但是邮件没有被发送。我使用的是基于文件的方法。邮件内容也成功地存储在一个文本文件中。

MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Subject: Password reset on 127.0.0.1:8000
From: webmaster@localhost
To: [email protected]
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



-------------------------------------------------------------------------------

但我没有收到邮件。希望能得到解决。最起码我想知道如何像django自动生成的那样,单独生成一次url。

设置.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 = '[email protected]'
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'),

]
python django
1个回答
0
投票

上面的代码是按照预期工作的。基于文件的Email_Backend用于在本地目录下存储邮件。而是 如果你想在你的gmail账户中接收邮件,请将你的后台设置修改为---------------------------------。django.core.mail.backends.smtp.EmailBackend

确保你的gmail账户的SMTP设置没有问题,并且允许访问django。https:/support.google.comaccountsanswer6010255?hl=en

你的settings.py应该是这样的。

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "[email protected]" # enter valid email address of which the smtp settings your configured
EMAIL_HOST_PASSWORD = "yourpassword"
EMAIL_PORT = 587
EMAIL_USE_TLS = True

上面的解决方案应该是可行的,但你也可以通过以下方式发送邮件。send_mail

subject = 'Thank you for registering to our site'
          message = 'this is a test message'
          email_from = '[email protected]'
          recipient_list = ['[email protected]', ]
          connection = [
              '[email protected]',
              'mypassword',
              False,
          ]
         send_mail(subject, message, email_from, recipient_list, connection)

为此你必须导入send_mail。https:/docs.djangoproject.comen3.0topicsemail。

© www.soinside.com 2019 - 2024. All rights reserved.