https://www.pythoneatstail.com/en/overview-all-articles/signup-and-password-reset-email-verification-allauth-django/

 

Signup and password reset with allauth's email verification in Django

This tells allauth that we want to authenticate via email, that an email field is required, that we also want to verify our email address by sending a verification link and that we will not use the username. Behind the scenes Django will still use a unique

www.pythoneatstail.com

 

 

1. base.py

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

인증 수단으로 username대신 email을 사용해야한다. 

 

 

2. local.py

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

email인증 서비스를 제공하기 위해 입력해주자. gmail에서는 프로필로 가서 보안 탭의 앱별 암호를 설정해서 입력해줘야한다. 

 

 

3. dev.py

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

allauth 이메일 인증을 사용하기 위해서 django email기능은 주석처리 하면 된다. 

 

 

4. forms.py

from django import forms
from django.utils.translation import gettext_lazy as _

class SignupForm(forms.Form):
    first_name = forms.CharField(max_length=30, label=_("First name"))
    last_name = forms.CharField(max_length=30, label=_("Last name"))
    display_name = forms.CharField(max_length=30, label=_("Display name"), help_text=_("Will be shown e.g. when commenting."))

    def signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.display_name = self.cleaned_data['display_name']
        user.save()

가입할 때, first, last, display이름을 입력받고 사용자 정보에 입력하기 위해 만들어준다. 

 

cleaned_data : 검증에 통과한 값을 사전타입으로 제공 (딕셔너리)

 

 

5. signup.html

{% extends 'account/base_card.html' %}

{% load i18n %}

{% block head_title %}{% trans "Sign Up" %}{% endblock %}

{% block card-header %}
<h3>{% trans "Sign Up" %}</h3>
{% endblock %}

{% block card-body %}

<form method="POST" action="{% url 'account_signup' %}" class="needs-validation" novalidate>
    {% csrf_token %}
    <div class="form-row">
        <div class="form-group col-md-6">
            {% with field=form.email %}{% include "account/form_field.html" %}{% endwith %}
        </div>
        <div class="form-group col-md-6">
            {% with field=form.display_name %}{% include "account/form_field.html" %}{% endwith %}
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-6">
            {% with field=form.password1 %}{% include "account/form_field.html" %}{% endwith %}
        </div>
        <div class="form-group col-md-6">
            {% with field=form.password2 %}{% include "account/form_field.html" %}{% endwith %}
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-6">
            {% with field=form.first_name %}{% include "account/form_field.html" %}{% endwith %}
        </div>
        <div class="form-group col-md-6">
            {% with field=form.last_name %}{% include "account/form_field.html" %}{% endwith %}
        </div>
    </div>
    <button type="submit" class="btn btn-outline-primary">{% trans "Sign Up" %}</button>
</form>

{% endblock %}

{% block card-footer %}
<p>{% trans "Already have an account?" %} <a href="{% url 'account_login' %}">{% trans "Sign In" %}</a></p>
{% endblock %}

 

 

추가)

사이트 주소를 바꾸고 싶다면 django-admin에서 site에서 example.com을 바꿔주고 base.py 에서 다음과 같이 수정하면 된다. 

 

BASE_URL = 'http://PythonEatsTail.com'

 

+ Recent posts