# user authentication

**URL:** <https://forum.djangoproject.com/t/user-authentication/14955>\
**Category:** Getting Started\
**Created:** [July 22, 2022, 9:52am UTC](https://forum.djangoproject.com/t/user-authentication/14955 "2022-07-22T09:52:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 22, 2022, 9:52am UTC](https://forum.djangoproject.com/t/user-authentication/14955/1 "2022-07-22T09:52:58Z")

</div>

hi, anyone help me with user authentication, i am unable to authenticate a user after signup, it let me login in admin site but as a normal user i am unable to login in website, sorry for the code i am unable to proper indent here.

````plaintext
    if request.method == "POST":
        fm = AuthenticationForm(request=request, data=request.POST)
        if fm.is_valid():
            username = fm.cleaned_data.get('username')
            password = fm.cleaned_data.get('password')
            user = authenticate(request,username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('/jobseekersprofile/')
    else:
        fm = AuthenticationForm()
    return render(request, 'jobglobel/userlogin.html', {'form': fm})```
````

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [July 22, 2022, 10:26am UTC](https://forum.djangoproject.com/t/user-authentication/14955/2 "2022-07-22T10:26:48Z")

</div>

To indent the code here you must use the backtick ` in the beggining and end of the line.  
For multilines use 3 backticks.

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 22, 2022, 11:02am UTC](https://forum.djangoproject.com/t/user-authentication/14955/3 "2022-07-22T11:02:57Z")

</div>

done thanx, do you have any idea what problem could be ?, i am unable to authenticate any normal user in website, when do login in admin site it allows to do login in admin site.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [July 22, 2022, 11:07am UTC](https://forum.djangoproject.com/t/user-authentication/14955/4 "2022-07-22T11:07:45Z")

</div>

Now that we had properly formatted code, it’s required some more information about what is happening.

Are you seeing any errors on the console, do you receive any errors on the page?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 22, 2022, 11:27am UTC](https://forum.djangoproject.com/t/user-authentication/14955/5 "2022-07-22T11:27:27Z")

</div>

We will need to see the complete view, and the template you are using (userlogin.html).

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 22, 2022, 11:34am UTC](https://forum.djangoproject.com/t/user-authentication/14955/6 "2022-07-22T11:34:48Z")

</div>

no i am not facing an any error, it just render me same login page, after putting right credentials.  
Here is Template which i am using

````auto
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Jobseekers Login</title>
    <link rel="stylesheet" type="text/css" href="{% static 'jobglobel/css/userlogin.css' %}">
    <link rel="stylesheet" type="text/css" href="{% static 'jobglobel/css/font.css' %}">
    <link rel="shortcur icon" type="image/png" href="{% static 'jobglobel/images/worldwide.png' %}">
</head>

<body>
    <div class="div00">
        <nav>
            <h2>WelCome To</h2>
            <a href="{% url 'home' %}"><img class="img1" src="{% static 'jobglobel/images/worldwide.png' %}" alt=""></a>
            <a class="divjoba1" href="{% url 'home' %}">JobGlobeL</a>
            <p class="navp">WorldWide</p>
            <p class="navp1">Not Registered Yet?</p>
            <a href="{% url 'usersignup' %}"><button class="btn1">SIGNUP NOW</button></a>
            <p class="navp2">Here.</p>
        </nav>
    </div>
    <div class="div1">
        <div class="div11">
            <p>JobSeeker's & Recruiters Login</p>
            <form class="formlogin" action="" method="POST" novalidate>
                {% csrf_token %}
                <div class="userdiv" >
                    <span class="sp" >* </span>{{form.username.label}} <br> {{form.username}} <span class="error" >{{form.username.errors|striptags}}</span>
                </div> 
                <div class="userpass">
                    <span class="sp" >* </span>{{form.password.label}} <br> {{form.password}} <span class="error" >{{form.password.errors|striptags}}</span> <br>
                </div>
                <input class="submitbtn" type="submit" value="LOGIN">
            </form>
        </div>
    </div>
</body>

</html>```
````

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 22, 2022, 11:53am UTC](https://forum.djangoproject.com/t/user-authentication/14955/7 "2022-07-22T11:53:08Z")

</div>

We still need to see the _complete_ view, and the form.

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 22, 2022, 12:06pm UTC](https://forum.djangoproject.com/t/user-authentication/14955/8 "2022-07-22T12:06:35Z")

</div>

````nohighlight
from django.shortcuts import redirect, render
from .forms import jobglobeluserform
from .models import jobglobeluser
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate,login,logout,views
from django.contrib.auth.decorators import login_required

def userlogin(request):
    if request.method == "POST":
        fm = AuthenticationForm(request=request, data=request.POST)
        if fm.is_valid():
            username = fm.cleaned_data.get('username')
            password = fm.cleaned_data.get('password')
            user = authenticate(request,username=username, password=password)
            if user is not None:
                login(request, user)
                return redirect('/jobseekersprofile/')
    else:
        fm = AuthenticationForm()
    return render(request, 'jobglobel/userlogin.html', {'form': fm})```

FORM I HAVE USED INBUILT FORM DJANGO ' AuthenticationForm()' As you can see in views.py 

FOR Template I have Posted Earlier. You Can check it above.

````

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 25, 2022, 7:46am UTC](https://forum.djangoproject.com/t/user-authentication/14955/9 "2022-07-25T07:46:32Z")

</div>

hiii, have you find any problem?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 25, 2022, 11:19am UTC](https://forum.djangoproject.com/t/user-authentication/14955/10 "2022-07-25T11:19:30Z")

</div>

I would use the Django shell to try that `authenticate` function call with the username and password that you are trying, to verify that the password is valid.

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 25, 2022, 11:22am UTC](https://forum.djangoproject.com/t/user-authentication/14955/11 "2022-07-25T11:22:50Z")

</div>

password is same as i kept while creating user for testing purpose., as normal user it is not allowing me to do login, but if i create superuser and do login in admin site, it is working.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 25, 2022, 11:26am UTC](https://forum.djangoproject.com/t/user-authentication/14955/12 "2022-07-25T11:26:50Z")

</div>

How did you specifically create the user and assign the password?

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 25, 2022, 11:31am UTC](https://forum.djangoproject.com/t/user-authentication/14955/13 "2022-07-25T11:31:11Z")

</div>

using model form by validing it in forms.py(built in method ), not created custom user model.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 25, 2022, 11:33am UTC](https://forum.djangoproject.com/t/user-authentication/14955/14 "2022-07-25T11:33:01Z")

</div>

Show me your view that creates your user.

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 25, 2022, 11:36am UTC](https://forum.djangoproject.com/t/user-authentication/14955/15 "2022-07-25T11:36:13Z")

</div>

```nohighlight
from django.shortcuts import redirect, render
from .forms import jobglobeluserform
from .models import user
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate, login, logout, views
from django.contrib.auth.decorators import login_required

def userregistration(request):
    if request.method == 'POST':
        fm = jobglobeluserform(request.POST, request.FILES)
        if fm.is_valid():
            useris = fm.cleaned_data.get('useris')
            firstName = fm.cleaned_data.get('FirstName')
            lastname = fm.cleaned_data.get('LastName')
            username = fm.cleaned_data.get('username')
            email = fm.cleaned_data.get('email')
            code = fm.cleaned_data.get('code')
            mobile = fm.cleaned_data.get('mobile')
            country = fm.cleaned_data.get('country')
            state = fm.cleaned_data.get('state')
            city = fm.cleaned_data.get('city')
            gender = fm.cleaned_data.get('Gender')
            password = fm.cleaned_data.get('password')
            rpassword = fm.cleaned_data.get('rpassword')
            resume = fm.cleaned_data.get('resume')
            reg = user(useris=useris, FirstName=firstName, LastName=lastname, username=username, email=email, code=code, mobile=mobile,
                                country=country, state=state, city=city, Gender=gender, password=password, rpassword=rpassword,
                                resume=resume)
            reg.save()
            global val

            def val():
                return firstName
            global val2

            def val2():
                return lastname
            return redirect('/success/')
    else:
        fm = jobglobeluserform()
    return render(request, 'jobglobel/usersignup.html', {'form': fm})
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 25, 2022, 11:37am UTC](https://forum.djangoproject.com/t/user-authentication/14955/16 "2022-07-25T11:37:50Z")

</div>

> [@Mihirpatel535](#):
>
> `reg = user(useris`

What is this `user(...` function? (Please answer with the actual code, not a description of it.)

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 25, 2022, 11:38am UTC](https://forum.djangoproject.com/t/user-authentication/14955/17 "2022-07-25T11:38:36Z")

</div>

class of model.py file which i have created for saving users in my website

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 25, 2022, 11:39am UTC](https://forum.djangoproject.com/t/user-authentication/14955/18 "2022-07-25T11:39:19Z")

</div>

Please show the code for this model.

---

<div class="post-metadata">

**Author:** ![Mihirpatel535](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mihirpatel535/32/9366_2.png) [@Mihirpatel535](https://forum.djangoproject.com/u/Mihirpatel535)\
**Post date:** [July 25, 2022, 11:39am UTC](https://forum.djangoproject.com/t/user-authentication/14955/19 "2022-07-25T11:39:59Z")

</div>

```nohighlight

class user(models.Model):
    joined_on = models.DateTimeField(auto_now_add=True)
    useris = models.CharField(max_length=30,null=True)
    FirstName = models.CharField(max_length=20)
    LastName = models.CharField(max_length=20)
    username = models.CharField(max_length=100)
    email = models.EmailField(unique=True,max_length=150)
    code = models.CharField(max_length=20)
    mobile = models.CharField(max_length=13)
    country = models.CharField(max_length=30)
    state = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    Gender = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    rpassword = models.CharField(max_length=20)
    resume = models.FileField(upload_to='docs/')
```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [July 25, 2022, 11:41am UTC](https://forum.djangoproject.com/t/user-authentication/14955/20 "2022-07-25T11:41:08Z")

</div>

Why do you think the `authenticate` function is going to check this model instead of the system-defined `User` model?

[Next page](https://forum.djangoproject.com/t/user-authentication/14955.md?page=2)
