Электронное письмо с подтверждением Allauth с пользовательской регистрационной формой?

#django #django-templates #django-authentication #django-allauth

#django #django-шаблоны #django-аутентификация #django-allauth

Вопрос:

У меня есть пользовательская регистрационная форма с моим собственным представлением регистрации. Я недавно начал использовать django-allauth и хотел бы добавить подтверждение электронной почты к своей регистрации. Возможно ли это?

просмотр регистрации:

 def registration(request):
    user_creation_form = UserProfileCreationForm(request.POST or None)
    if request.method == 'POST':
        if user_creation_form.is_valid():
            user_creation_form.save()
            username = user_creation_form.cleaned_data['username']
            password = user_creation_form.cleaned_data['password2']
            user = authenticate(username=username, password=password)
            login(request, user)
            messages.add_message(request, messages.SUCCESS, YOU_HAVE_BEEN_REGISTERED)
            return HttpResponseRedirect(reverse('homepage'))
    return render(request, 'dolava/accounts/registration.html',
                  context={'user_creation_form': user_creation_form})
  

Вот как переписать шаблоны allauth:

в SETTINGS.PY

 TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)


TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates','allauth'),)
  

основные шаблоны приложений:

 templates/account/password_reset.html (overriden allauth templates) 
  

Проблема в том, что я хочу иметь много полей в шаблоне регистрации type_of_user telephone , например, и т.д.

registration.html:

 {% extends 'base.html' %}
{% load static %}

{% block head %}
    <script src="{% static "js/registrationToggleFields.js" %}"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>


    <script type="text/javascript">
        $(document).ready(function () {
            $('#id_country').select2();
            $('#id_telephone_0').select2();
        });


    </script>
{% endblock %}

{% block content %}


    <div class="col-md-12 text-center">
    <h2>Register your account</h2>

    <hr class="col-md-12 blackhr no-bottom-margin">
    </div>


{#    <div class="col-md-12 text-center center-block">#}

        <form action="" method="post" class="col-md-6 col-md-offset-3" id="register-form">
            {% csrf_token %}
            {{ user_creation_form.non_field_errors }}

            <div id="form-account-type">
                <h3 align="center" class="main-color">Account Type</h3>
                <hr>

{#                <div class="col-md-12">#}

                    <div id="type-of-user-field-wrapper" class="fieldWrapper">

                        <div>
                        <label for="{{ user_creation_form.type_of_user.id_for_label }}">Type of user:</label>
                        {% if user_creation_form.type_of_user.field.required %} * {% endif %}
                        </div>

                        {{ user_creation_form.type_of_user }}
                        {{ user_creation_form.type_of_user.errors }}
                    </div>

                    <div id="country-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.country.id_for_label }}">Country:</label>
                        {% if user_creation_form.country.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.country }}
                        {{ user_creation_form.country.errors }}
                    </div>
{#                </div>#}

            <hr>
            </div>



            <div id="form-login-credentials" class="form-group-container show-allways">
                <h3 align="center" class="main-color">Credentials</h3>
                <hr>

                <div>
                    <div id="username-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.username.id_for_label }}">Username:</label>
                        {% if user_creation_form.username.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.username }}
                        {{ user_creation_form.username.errors }}
                    </div>

                    <div id="password1-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.password1.id_for_label }}">Password:</label>
                        {% if user_creation_form.password1.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.password1 }}
                        {{ user_creation_form.password1.errors }}
                    </div>

                    <div id="password2-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.password2.id_for_label }}">Confirm your password:</label>
                        {% if user_creation_form.password2.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.password2 }}
                        {{ user_creation_form.password2.errors }}
                    </div>
                </div>
            <hr>
            </div>


            <div id="form-personal-information" class="form-group-container show-personal">
                <h3 align="center" class="main-color">Personal information</h3>
                <hr>

                <div>
                    <div id="first-name-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.first_name.id_for_label }}">First name:</label>
                        {% if user_creation_form.first_name.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.first_name }}
                        {{ user_creation_form.first_name.errors }}
                    </div>

                    <div id="last-name-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.last_name.id_for_label }}">Last name:</label>
                        {% if user_creation_form.last_name.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.last_name }}
                        {{ user_creation_form.last_name.errors }}
                    </div>

                </div>
            <hr>
            </div>



            <div id="form-company-information" class="form-group-container show-company-sk show-company">
                <h3 align="center" class="main-color">Company information</h3>
                <hr>

                <div>
                    <div id="company-name-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.company_name.id_for_label }}">Company name:</label>
                        {% if user_creation_form.company_name.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.company_name }}
                        {{ user_creation_form.company_name.errors }}
                    </div>
                    <div id="address-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.address.id_for_label }}">Address:</label>
                        {% if user_creation_form.address.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.address }}
                        {{ user_creation_form.address.errors }}
                    </div>
                </div>
            <hr>
            </div>



            <div id="form-billing-information" class="form-group-container show-company-sk">
                <h3 align="center" class="main-color">Billing information</h3>
                <hr>

                <div>
                    <div id="ico-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.ICO.id_for_label }}">IČO:</label>
                        {% if user_creation_form.ICO.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.ICO }}
                        {{ user_creation_form.ICO.errors }}
                    </div>
                    <div id="dic-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.DIC.id_for_label }}">DIČ:</label>
                        {% if user_creation_form.DIC.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.DIC }}
                        {{ user_creation_form.DIC.errors }}
                    </div>
                </div>
            <hr>
            </div>



            <div id="form-contact-information" class="form-group-container show-allways">
                <h3 align="center" class="main-color">Contact information</h3>
                <hr>

                <div>
                    <div id="email-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.email.id_for_label }}">Email:</label>
                        {% if user_creation_form.email.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.email }}
                        {{ user_creation_form.email.errors }}
                    </div>
                    <div id="telephone-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.telephone.id_for_label }}">Telephone:</label>
                        {% if user_creation_form.telephone.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.telephone }}
                        {{ user_creation_form.telephone.errors }}
                    </div>
                    <div id="fax-field-wrapper" class="fieldWrapper">

                        <label for="{{ user_creation_form.fax.id_for_label }}">Fax:</label>
                        {% if user_creation_form.fax.field.required %} * {% endif %}
                        <br>
                        {{ user_creation_form.fax }}
                        {{ user_creation_form.fax.errors }}
                    </div>

                </div>


            </div>



            <div class=" clearfix top-margin">
                <button class="btn btn-success center-block" style="width:100%; padding: 15px 0;" type="submit">
                    Register
                </button>
            </div>

        </form>

{#    </div>#}



{% endblock %}
  

У вас есть какие-либо советы?

Ответ №1:

Вы должны переопределить один класс, а затем предоставить все необходимые переменные для конкретного шаблона. Представление, которое возвращает confirmation_email ConfirmEmailView allauth/account/views.py , переопределяет этот класс, pass all the required variables from view to the template а затем переопределяет ваш шаблон account/email_confirm для отображения требуемых данных.