I want to make login and registration for a custom user with only 5 fields: user name, name, password, linkedin id and mobile number.
I made registration successfully but I am stuck with login, I cannot authenticate my user. Is there any way to authenticate my user, or how can I login? Currently I am getting logged in by
user = Consultants.objects.get(Q(username= username) & Q(password= password)) But i want to make login by
user=authenticate(username=username,password=password) Note:I don't want to use django default User Model For it. Please help me in this. Thanks in advance.
models.py
class Consultants(models.Model): first_name=models.CharField(max_length=255,blank=True,null=True) username=models.CharField(max_length=255,blank=True,null=True) password=models.CharField(max_length=50,blank=True,null=True) mobile_no=models.CharField(max_length=255,blank=True,null=True) linkedin_id=models.CharField(max_length=255,blank=True,null=True) is_active=models.BooleanField(default=False) views.py
def register(request): context = RequestContext(request) registered = False print "inside register view" if request.method == 'POST': consultant_form = ConsultantsForm(data=request.POST) if consultant_form.is_valid(): consultant = consultant_form.save(commit=False) consultant.save() registered = True else: print consultant_form.errors else: consultant_form = ConsultantsForm() return render_to_response( 'register.html', {'consultant_form': consultant_form, 'registered': registered}, context_instance=RequestContext(request)) def login_user(request): context = RequestContext(request) if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] print type(username) try: user = Consultants.objects.get(Q(username= username) & Q(password= password)) user = authenticate(username=username, password=password) if user.is_active: user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) a= request.user.username return HttpResponse("welcome......you are succesfuly log in") else: return HttpResponse("Your account is disabled.") except ObjectDoesNotExist: return HttpResponse("INvalid User")