1

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")

2 Answers 2

1

Note:I don't want to use django default User Model For it Please help me in this. Thanks inadvance

Is your Consultants class inheriting from the base Django user class?

The authenticate() function is used to authenticate the base user model, you may not be setting a password for the user when they are created?

Another way to go about this would instead create a Profile model with all of these extra fields with a OneToOneField to the base user model, and authenticate though the Django ORM.

Sign up to request clarification or add additional context in comments.

4 Comments

i don't want to use base class.i have a class Consultants with all field
Could you post your custom user model, signup form, and login view?
The reason you may be getting errors is because you are using django authenticate() where it expects a base auth User. If you with to continue with a custom login. You'll need to modify some middleware: docs.djangoproject.com/es/1.9/topics/auth/customizing/…
I have none offhand but custom authentication is outlined in the docs I linked. Also it would be much easier (and much safer security wise) to use the base django user model for authenticate()
0

I think it's better to inheriting from django embedded user class, you can follow these steps:

  • extend your custom user class from AbstractBaseUser and PermissionsMixin
  • Assign and fill this config in settings.py:

    AUTH_USER_MODEL = 'YOUR_CUSTOM_CLASS_IN_FULL_QUALIFIED_NAME' e.g.: 'your_app_name.Consultants'

Voila, you can use django default user crud, also authentication

2 Comments

can u give me link where i can see full implementation of this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.