0

Is there any way I can use email as user name with out specifying custom user model. I am new to django, From past few hour I am searching and trying to find a way. I try creating custom modal but then everything get changed like admin login.

Secondly is there any way to define a custom model for specific app. eg. fronted only. So that back-end stay as is.

I just need to use email as username for users login.

Is it possible to use

USERNAME_FIELD = 'email'
Waqas
  • 637
  • 8
  • 19
  • http://stackoverflow.com/questions/5773970/django-auth-user-with-unique-email – M.javid Feb 25 '15 at 05:40
  • This is a hack. I am wondering if there is proper solution. – Waqas Feb 25 '15 at 05:43
  • http://stackoverflow.com/questions/15668124/can-i-change-the-username-field-in-django-1-5-without-creating-a-custom-user#15681648 – M.javid Feb 25 '15 at 05:49
  • @Waqas If you want i can provide the details for how to login using email and without username... – Raja Simon Feb 25 '15 at 05:51
  • Why doesn't USERNAME_FIELD work for you? Is it because your email field is not unique? Or because you want to be able to login by username too? – Enrico Feb 25 '15 at 06:12
  • For user name field don't I have to define custom model? Which will again change everything. – Waqas Feb 25 '15 at 15:21

2 Answers2

1

I actually end up using django-authtools

Waqas
  • 637
  • 8
  • 19
0

Get the email and password using form

email = form.cleaned_data['email']
password = form.cleaned_data['password']

If the given email matches with exiting record.

try:
    user = User.objects.get(email=email)
    if user.check_password(password):
        username = user.username
        user = authenticate(username=username, password=password)
        login(request, user)
        messages.success(request, "Welcome {}".format(email))
    else:
        messages.error(request, "Password not match for  {}".format(email))
except User.DoesNotExist:
    pass
Raja Simon
  • 10,126
  • 5
  • 43
  • 74