django rest auth email validation

我是研究僧i 提交于 2021-01-20 20:15:56

问题


Can someone explain how to set up email verification for django rest auth ? My settings.py contains:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

And I've added this line in my urls.py:

url(r'^accounts/', include('allauth.urls')),

But when I register with this endpoint: rest-auth/registration/, the email is not sent, but the user is created. What is the good configuration to send email confirmation? Do I have to have a SMTP server?

EDIT: (after @McAbra comment)

Obviously, this setting cannot send email:

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

It only print the mail in the django console output. The settings given by @McAbra works well. But in a production environment, is it a good practice to send verification email with gmail? Have I just to create email such noreply.myapp@gmail.com?

EDIT 2:

My other problem is that the sent mail is empty. The template are presents in site-packages\allauth\templates\account\email. The email title is [http://localhost:8000/] but there is no content. I have no error in the python console. Any idea?

Solution for this point:

site-packages\allauth\templates\account\email files must be copied in your-app\templates\account\email


回答1:


You need to have a SMTP server if you want to send real emails. I like to use Gmail SMTP server. settings.py would be:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'gmail_username'
EMAIL_HOST_PASSWORD = 'password from https://security.google.com/settings/security/apppasswords'

Try from there. I'll try to help further if that doesn't help.




回答2:


I had problems with sending emails and after that with the link in the email. You need to explicitly set the account confirm email view.

Below are my settings in settings.py

 ACCOUNT_AUTHENTICATION_METHOD = 'email'
 ACCOUNT_EMAIL_REQUIRED = True   
 ACCOUNT_USERNAME_REQUIRED = False
 ACCOUNT_EMAIL_VERIFICATION = 'mandatory'

in urls.py

  from allauth.account.views import confirm_email as allauthemailconfirmation

  urlpatterns  = [
  url(r'^api/rest-auth/account-confirm-email/(?P<key>[-:\w]+)/$',allauthemailconfirmation,
    name='account_confirm_email'),
  ]

You can use mailcatcher.me if you want to check emails locally.



来源:https://stackoverflow.com/questions/39983504/django-rest-auth-email-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!