python - Creating a custom login for user changed the login for my Admin. How to prevent that? -


i creating web application users have register , create profiles. using "abstractbaseuser" class provided django, wanted add other fields. now, when user logs in, want login credentials mobile number , password. created custom authentication function , registered in settings.py. problem changed login admin sit, want remain same.

i followed tutorial add custom fields user link

my models.py:

from django.db import models django.contrib.auth.models import abstractbaseuser django.contrib.auth.models import usermanager    class userinfo(abstractbaseuser):      email = models.emailfield('email address', unique=true,)     mobile = models.charfield(max_length=15, unique=true,)     address = models.charfield(max_length=500)     landmark = models.charfield(max_length=50)     status = models.booleanfield(default=false)     is_active = models.booleanfield(default=true)     is_admin = models.booleanfield(default=false)      username_field = 'mobile'       def __unicode__(self):         return self.get_username() 

views.py login page:

import datetime .forms import userinfoform, loginform django.shortcuts import render, render_to_response .models import userinfo, orderhistory django.http import httpresponseredirect, httpresponse django.template import requestcontext, loader django.contrib.auth import login django_login, authenticate, logout django_logoutdef login(request):  """ log in view """ if request.method == 'post':     form = loginform(data=request.post)     print ("step 1")     if form.is_valid():         print ("step 2")         user = authenticate(mobile=request.post['username'], password=request.post['password'])         print ("step 3")         if user none:             print("no valid user")         if user not none:             print ("step 3.10")             if user.is_active:                 print ("step 4")                 django_login(request, user)                 print ("step 5")                 return redirect('/i/home') else:     form = loginform() return render_to_response('loginpage.html', {     'form': form, }, context_instance=requestcontext(request))` 

forms.py:

from django import forms  .models import userinfo, orderhistory   django.forms import modelform   class loginform(forms.form): """ login form """ mobile = forms.charfield(widget=forms.textinput) password = forms.charfield(widget=forms.passwordinput())  class meta:     fields = ['mobile', 'password']        ` 

the backend created:

from django.conf import settings django.contrib.auth.models import check_passwor .models import userinfo  class emailauthbackend(object):     """     custom authentication backend. allows users log in using            email address.     """  def authenticate(self, mobile=none, password=none):     """     authentication method     """     try:         user = userinfo.objects.get(mobile=mobile)         if user.check_password(password):             return user     except userinfo.doesnotexist:         return none  def get_user(self, user_id):     try:         user = userinfo.objects.get(pk=user_id)         if user.is_active:             return user         return none     except userinfo.doesnotexist:         return none 

and changes made settings.py:

auth_user_model = 'registration.userinfo' authentication_backends = ['registration.authbackend.emailauthbackend', ] 

the default backend django ('django.contrib.auth.backends.modelbackend',), django admin using. when insert line:

authentication_backends = ['registration.authbackend.emailauthbackend', ] 

you're overwriting default backend, rather adding second backend option. change line to:

authentication_backends = (     'registration.authbackend.emailauthbackend',     'django.contrib.auth.backends.modelbackend', ) 

and application should pick second backend. may need trigger second backend based on logic whether first succeeds, see responses questions django multiple authentication backend 1 project, how? or the docs here.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -