Allauth确认电子邮件与自定义注册表格?

时间:2016-10-17 10:05:20

标签: django django-templates django-authentication 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_usertelephone等。

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 个答案:

答案 0 :(得分:0)

您必须覆盖一个类,然后将所有必需的变量提供给特定模板。返回confirmation_email ConfirmEmailView allauth/account/views.py的视图将覆盖此类pass all the required variables from view to the template,然后覆盖您的模板account/email_confirm以显示所需数据。