Django-Registration: Email as username

前端 未结 3 1751
攒了一身酷
攒了一身酷 2021-01-29 23:51

How do I use emails instead of username for authentication using django-registration.

Further how do I enforce this change once I have already installed the registration

3条回答
  •  滥情空心
    2021-01-30 00:08

    The simplest way would be to hide the username field in the registration_form template with CSS display:none property. And add a script which takes the value from the email form field and sets the value in the hidden username field. So while submitting the form, the user submits the email as username without knowing it. The only problem is since a username field, by default, cannot accept an email which is longer than 30 characters. So, for safe bet, you should increase the username length manually in your database.

    Here is the new registration_form.html template:

    {% csrf_token %}
    {% if form.username.errors %} {{ form.username.errors.as_text }} {% endif %}
    {{ form.username }}
    {% if form.email.errors %} {{ form.email.errors.as_text }} {% endif %}
    {{ form.email }}

    {% if form.password1.errors %} {{ form.password1.errors.as_text }} {% endif %}
    {{ form.password1 }}

    {% if form.password2.errors %} {{ form.password2.errors.as_text }} {% endif %}
    {{ form.password2 }}

    The Simple jQuery srcipt that sets the value in the hidden form field username is:

    $(document).ready(function()
    {
      $('#id_email').blur(function(e)
      {
        var email_address = $('#id_email').val();
        $('#id_username').val(email_address);
       });
    });

    The ids id_email and id_username are set by the django-registration form automatically.

提交回复
热议问题