Login

Email Auth

Author:
petzah
Posted:
April 26, 2014
Language:
Python
Version:
1.6
Tags:
email auth
Score:
0 (after 0 ratings)

Yet another authentication by email address. This one is quick and dirty as we are saving email address in both Username and Email fields. For proper way how to deal with it see

https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#auth-custom-user

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# forms.py:

from django.contrib.auth.forms import UserCreationForm
from django.core.validators import validate_email
from django import forms

class UserRegisterForm(UserCreationForm):
    # we are using email as username so override label and validators
    username = forms.CharField(
        label = "Email:",
        max_length = 30,
        required = True,
        validators=[validate_email],
    )
# ====================================================
# views.py:

from django.views.generic.edit import FormView
class UserRegister(FormView):
    template_name = 'form_general.html'
    form_class = UserRegisterForm
    success_url = '/user/'

    def form_valid(self, form):
        # we are using email as username so let's copy it also to email field
        user = form.save(commit=False)
        user.email = user.username
        user.save()
        return super(UserRegister, self).form_valid(form)

More like this

  1. Login with email or username by zeeg 6 years, 8 months ago
  2. Email or username authentication with masquerading by petrilli 5 years, 10 months ago
  3. Use email addresses for user name for django 1.3 by kidzik 3 years, 10 months ago
  4. Custom Auth Backend to use E-Mail in Django by bsoculd 3 weeks, 6 days ago
  5. Simple e-mail template tag by dchandek 7 years ago

Comments

tgandor (on May 11, 2014):

30 characters is quite short for an e-mail address. I have a similar hack, and the first thing I installed was the 'longerusername' application.

#

Please login first before commenting.