Djangoroidの奮闘記

python,django,angularJS1~三十路過ぎたプログラマーの奮闘記

Django django-registration-redux + sendgrid 導入方法

概要

django-registration-redux + sendgridを導入してみる。

参考サイト

Quick start guide — django-registration-redux 1.4 documentation

Django Heroku メールの設定 - Djangoroidの奮闘記

やってみたこと。

  • django-registration-redux をinstall
  • django-registration-redux のdjangoでの設定
  • django-registration-redux を試してみる。
  • sendgridをdjangoに設定する。
  • django-registration-redux と連動するか確認する。

django-registration-redux をinstall

pip install django-registration-redux

django-registration-redux のdjangoでの設定

  • 静的ページのテンプレートをそのままdjangoのtemplatesフォルダにコピーして配置する。

ここのregistrationフォルダのhtmlファイルをそのまま持ってきた。

Try-Django-1.8/src/templates/registration at master · codingforentrepreneurs/Try-Django-1.8 · GitHub

  • registrationと、django.contrib.sites をInstalled_appsに追加しておく。
INSTALLED_APPS = (
    'django.contrib.sites',
    'registration', #should be immediately above 'django.contrib.auth'
    'django.contrib.auth',
    # ...other installed applications...
)
  • django-registration-reduxの設定をしてみる。
ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window; you may, of course, use a different value.
REGISTRATION_AUTO_LOGIN = True # Automatically log the user in.
  • とりあえずこの辺で、python manage.py migrateして、runserverしてみる。

Error発生!!

django.contrib.sites.models.DoesNotExist: Site matching query does not exist.

うーん、、、statck over flowで調べたら以下の回答があったので、その通り設定してみる。

Why does removing 'django.contrib.sites' from INSTALLED_APPS fix the 'DoesNotExist at /admin/' error that I suddenly got? - Stack Overflow

SITE_ID = 1

再度、runserverしてみる。。。表示された!!

  • ちゃんと機能しているかadminユーザーで、ログインして試してみる。

ログインはしたけど、http://127.0.0.1:8000/accounts/profile/に飛ばされて、profileのページは設定されていないという表示がされた。まあ、とりあえず、ログイン認証はうまくいってるぽいな!

  • 新規登録はできるか確認する。

新規登録をしてみる。。。Error発生!

ConnectionRefusedError at /accounts/register/

多分、smpt設定してないからだろうな。。。

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'yourgmail@gmail.com'
EMAIL_HOST_PASSWORD = 'yourpassword'
EMAIL_PORT = 587
EMAIL_USE_TLS = True

とりあえず、試しにgmailで設定してみた。

再度try。。。とりあえずメールは送れた! アクティベートのリンクをクリックしてみる。。。。example domainという表記が出た! とりあえず成功なはず! SITE_ID=1と設定していたので、SITE_ID 1のexample domainに接続されたと思われる。

なので、SITE_ID 2 に、127.0.0.1:8000と登録して、settings.pyをSITE_ID=2としておく。

再度トライ。。。OK!これで一旦、django-registration-reduxは登録完了。 sendgridをやってみよう。

sendgridをdjangoに設定する。

  • sendgridでアカウント設定を済ませて、以下のような感じで、設定する。
    EMAIL_HOST = 'smtp.sendgrid.net'
    EMAIL_HOST_USER = os.environ['SENDGRID_USERNAME']
    EMAIL_HOST_PASSWORD = os.environ['SENDGRID_PASSWORD']
    EMAIL_PORT = 587
    EMAIL_USE_TLS = True

django-registration-redux と連動するか確認する。

accounts/register/に登録して、確認してみる。。。できた〜!!